Reputation: 1711
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
Upvotes: 2
Views: 269
Reputation: 238461
As i understand it the srand function takes a value - the seed value and returns a pseudo-random integer.
Almost, but not quite. It just sets the seed and returns void. rand
returns a pseudorandom integer.
Firstly what is a pseudo-random integer?
A pseudorandom integer is an integral output of a pseudorandom number generator. Pseudorandom number generator is an algorithm which generates a sequence of superficially random numbers.
Also do we need to initialize a time value? can we simply call the time function?
There is no need to initialize anything to call time
function.
I believe the time function returns something like the partial seconds
Not partial. It returns the number of seconds since epoch.
For further information, consult the reference documentation:
For basic information about pseudorandom number generators, see for example wikipedia: http://en.wikipedia.org/wiki/Pseudorandom_number_generator
Upvotes: 2
Reputation: 19855
Pseudo-random numbers are a deterministic (i.e., non-random) sequence of values produced algorithmically. Historically, a common way of doing this was to use a simple recurrence relationship: Ui = f(Ui-1), where the Ui's are integer values. A moment's thought tells you that as soon as you see a duplicate value for U, all subsequent values will also be duplicates. This means that the sequence cycles. A good generator is one where the U's appear to be uncorrelated, are uniformly distributed over the range of integers, and which has a long cycle length. Seeding the generator consists of picking an entry point in the cycle. If you always pick the same entry point, you will always get the same sequence. If you don't explicitly pick an initial state (seed value), then most PRNG's have a default initialization.
Over the years, it was recognized that the recurrence functions which were common in the 1950's-1980's had a variety of problems which made it easy to distinguish them from true randomness. More modern generators use a larger state space than a single integer, and project the high-dimensional space down to a single integer output on each invocation. That means you can get individual repeats of values but won't get a repeat of the sequence until you've returned to the same state space you started from. Since state spaces grow exponentially in the number of bits, this gives much longer usable sequences. Seeding still consists of initializing the state space.
Upvotes: 2
Reputation: 12573
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).
Upvotes: 5
Reputation: 49920
This is a way of insuring you get a different sequence of random numbers with each run, since (presumably) time() will have a different value with each run; you can (overly simplistically) think of the seed as the starting point for that sequence.
Upvotes: 2