Advice on a simple random number generation problem.
I have a simple integer vector, with only 4 values. I want to loop through the vector, assigning either 0 or 1 to each value. I want it to be random to the point that its different every time.
I thought the following would suffice:
for (int i = 0; i < (int)numberVect.size(); i++)
{
numberVect[i] = rand() % 2;
}
However, what I found was that every time I would close and re-open the program, the exact same values would be assigned.
So the first time through it would be 1,0,0,0, then if I ran the loop again without closing the program it would be 0,1,0,1. which seems perfect.
HOWEVER, after closing the program and restarting it up again, I would find the first sequence would again be 1,0,0,0 and the second would again be 0,1,0,1.
Also I have a second question in relation to this problem:
Can you suggest a way to make sure that AT LEAST one of the values in the vector is a开发者_运维知识库 1? while still allowing the random generation to work seamlessly?
Thanks a lot and I hope you guys can help :D
Question 1
You need to seed your random number generator with srand.
The sequence of pseudo-random numbers generated depends on the seed value (and are thus reproducible with the same seed value). I use the the current time as the seed value:
srand(time(NULL));
Question 2
There are only 16 possible sequences for you to choose from (4 bits).
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
Out of these, you want to randomly select one with at least 1
high bit. Therefore you just need to randomly select a number from 1 to 15 and then convert it to binary!
There are two ways of doing this:
Method 1:
int R = rand() % 15 + 1;
Method 2:
float r = static_cast<float>(rand())/RAND_MAX;
int R = static_cast<int>( ceil(1 + (15-1)*r) );
Once you have R
, convert it to binary. Here's a sample program with Method 1:
// Seed the random number generator
srand(static_cast<unsigned int>(time(NULL)));
// Get the random number from 1 - 15
int R = (rand()%15)+1;
cout<<R<<endl;
// Convert to binary
vector<int> numberVect(4);
for (int i = static_cast<int>(numberVect.size())-1; i >=0 ; i--)
{
numberVect[i] = R%2;
R/=2;
}
// Display
cout<<numberVect[0]<<","<<numberVect[1]<<","<<numberVect[2]<<","<<numberVect[3]<<endl;
Note: static_cast<int>
(x)
is the C++ way of doing (int)x
if you didn't know.
You should seed your random generator with the current time at program start to avoid this behaviour.
You need to seed the random generator, before using it. For that you need srand()
I usually do that with passing current time to srand()
as:
#include <time.h>
srand(time(NULL)); //seeding!
//now use
int whatever = rand();
Now everytime, you run this code you'll get different random sequence,even if there is one second of difference between two consecutive runs.
You need to seed your random numbers. Using the time is common practice.
Do this in your program before you generate any random numbers:
srand(time(NULL));
The random number generator isn't "truly random", it needs to start somewhere, and typically people will use the current system time as a good "kinda random" start point, guaranteeing that each run of the program will be different.
Your second question: can you make sure that at least one of the values is a "1" while still being random? Well, no, of course not, then it wouldn't be random. :) You can post-process, though, if you're willing to forgo some of the "randomness"-- after you generate the vector, check to see if there's at least one "1" value. If not, set one of them to "1". :)
You have to initialize the random seed. Include stdlib.h and time.h, then call this before calling rand() for the first time:
srand(time(NULL));
This code initializes the random seed with the current system time, in seconds, so you always get different results (unless you start the program twice during one second.)
You're getting the same results each time you run the program because you aren't seeding rand. It's usually recommended to seed based on time:
srand(time(NULL));
To make sure at least 1 number is 1, you could use a bool to indicate if you've gotten a 1 yet. If at the end of the array, you don't have a 1, do a final call to rand with a mod of the vector size, and set that value to 1.
精彩评论