rand() seed

I understand the concept with rand() and seed, but what exactly is seed? I know what it does, but I don't know what it is.

If anyone can tell me, I would be grateful :)
it's a number (or a series of numbers)


EDIT: to elaborate further...

pRNGs are typically mathematical equations. A simple example would be:

OUT = (X * IN) + Y;

Where 'X' and 'Y' are constants (typically prime numbers).

'OUT' is both
- your random number
and
- 'IN' for the next time the formula is run.

The "seed" is just the initial value for 'IN'.


Of course this is a simplistic example. More complex RNGs will work differently and may require larger/more seeds. But the concept is more or less the same.

EDIT2:

doh, Skillless replied before my edit.
Last edited on
rand uses the previous random number to generate a next one, if you set the seed to a value, and then call rand, youll get a number. If you seed it again with the same value, youll get the same number again.

its the reason people usually srand(time(NULL)), to set the seed to the current time, wich is always different, so that a program will always have different random numbers
(if you dont srand(), and output rand() 10 times, your program will output the same 10 numbers everytime)
Last edited on
wow that is interesting I never knew that before about the input being the output from the previous iteration. I just had always assumed that srand() took the seed and generated a series based on some propriety mathematical formula, and kept track of the index of the series inside a private variable that was incremented each time rand() was called. I guess it makes more sense the way you describe though, but would it be possible the other way too? Also, how is it possible to srand(seed1, seed2, seed3) as disch saids... can you specify an unspecified amount of parameters?
No I don't think thats right. I just tried it the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdlib.h>
#include <iostream>

using namespace std;

int main()
{
    srand(1);
    cout << "rand() = " << rand() << endl;
    cout << "rand() = " << rand() << endl;
    srand(41);
    cout << "rand() = " << rand() << endl;
    cout << "rand() = " << rand() << endl;
    return 0;
}


and got the following as output:

rand() = 41
rand() = 18467
rand() = 172
rand() = 22420

Process returned 0 (0x0)   execution time : 0.033 s
Press any key to continue.


If it takes as input the last iterations output then the output 172 should have been equivalanet to the output of 18467
Disch wrote:
Of course this is a simplistic example. More complex RNGs will work differently and may require larger/more seeds. But the concept is more or less the same.


I wasn't describing rand() in particular, I was describing the general overall concept of how RNGs work.

The output may not be used as the input directly, but it is used to affect the next input somehow.

wtf wrote:
Also, how is it possible to srand(seed1, seed2, seed3) as disch saids


I never said this.

What I said is that other RNGs might take larger/more seeds. Again I was speaking of RNGs in general, not rand(). rand() is just one RNG. There are tons of others.

In general the seed needs to be at least as big as the period of the RNG. So if you have an RNG with a period of 2^128, then it needs to have a seed that's at least 2^128.

rand() doesn't have that large of a period, so its seed is smaller.
Last edited on
Topic archived. No new replies allowed.