I'm very confused - I have the following code - the min and max are our range values. As i understand it the srand function takes a value - the seed value and returns a pseudo-random integer. Firstly what is a pseudo-random integer?
// Constants
const int MIN = 50;
const int MAX = 450;
// Get the system time.
unsigned seed = time(0);
// Seed the random number generator.
srand(seed);
// Generate two random numbers.
int num1 = MIN + rand() % MAX;
int num2 = MIN + rand() % MAX;
Also do we need to initialize a time value? can we simply call the time function? I believe the time function returns something like the partial seconds - creating our random #? I'm generally confused how the random # is generated.
Thanks
A "pseudo-random integer" means that it's generated from a function (so it is, in theory, predictable) but it would not be easy to distinguish between the output and a truly random value (e.g. if a monkey takes one of the numbers 0-9 from a bag and throws it back afterwards). Depending on how good the pseudo random function is, of course. Standard C/C++ rand() is not good enough for the "serious" crypto stuff, by the way.
If you don't initialize srand with a seed, you will always get the same sequence of output values from the number generator. Time value is used because of the assumption that the "partial seconds" part will be relatively random (and it's relatively cheap to obtain).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With