|
-
August 27th, 2010, 07:12 AM
#1
Help with generating random number
If I want to generate 10 random numbers ranging from 0 to 9, is it possible to have 10 different numbers every time? For example, every time they should be like this, 1,3,4,9,0,2,8,7,5,6 without duplicate. I tried srand and rand but it looks like it doesn't work. They simply will generate duplicate numbers. Thanks for your inputs.
-
August 27th, 2010, 07:17 AM
#2
Re: Help with generating random number
Yes, by shuffling a container of those numbers.
-
August 27th, 2010, 07:33 AM
#3
Re: Help with generating random number
http://www.cplusplus.com/reference/a...andom_shuffle/
You'll even notice that the example is your problem (minus the 0).
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
August 27th, 2010, 08:26 AM
#4
Re: Help with generating random number
Thanks for your inputs. I guess you meant random_shuffle. But random_shuffle generates the same result every time. I wonder if it is possible to generate different result every time by using random_shuffle. It looks like it doesn't accept seed. Thanks for your inputs.
 Originally Posted by laserlight
Yes, by shuffling a container of those numbers.
-
August 27th, 2010, 08:39 AM
#5
Re: Help with generating random number
 Originally Posted by dullboy
Thanks for your inputs. I guess you meant random_shuffle. But random_shuffle generates the same result every time. I wonder if it is possible to generate different result every time by using random_shuffle. It looks like it doesn't accept seed. Thanks for your inputs.
I tried the example in the link above. It generates different numbers for me.
-
August 27th, 2010, 08:41 AM
#6
Re: Help with generating random number
 Originally Posted by GCDEF
I tried the example in the link above. It generates different numbers for me.
unfortunately, random_shuffle is not guaranteed neither to seed itself nor to use rand internally, therefore using srand() will have no effect in general. So, you should specify your own random number generator in the random_shuffle call.
EDIT: oh sorry, I didn't realized that the example in the link already covered the user provided random generator case ...
Last edited by superbonzo; August 27th, 2010 at 08:51 AM.
-
August 27th, 2010, 08:55 AM
#7
Re: Help with generating random number
Computers cannot generate random number , because behind each rand() algorithm there a sequence , for further reading pls. see this
http://en.wikipedia.org/wiki/Random_number_generation
-
August 27th, 2010, 11:00 AM
#8
Re: Help with generating random number
 Originally Posted by monarch_dodra
That is a good example. How does p_myrandom work in the example? It looks like p_myrandom is a function pointer applied to each element of the vector. But how'd I understand i in the definition of myrandom?Thanks for your inputs.
-
August 29th, 2010, 04:22 AM
#9
Re: Help with generating random number
You could try this:
Code:
srand(time(0));
int a[10] = {0};
int n;
bool duplicate;
bool loop;
for(int i = 0; i <= 9; ++i)
{
loop = true;
while(loop)
{
n = 1+rand()%10;
duplicate = false;
for(int j = 0; j <= 9; ++j)
{
if(a[j] == n)
duplicate = true;
}
if(duplicate == false)
{
a[i] = n;
loop = false;
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|