473,238 Members | 1,485 Online

# rand seed in C (explain a code)

82
Hello everyone,I don't speak very good english , so sorry if i make any mistake
Can anyone explain me this code
[PHP]unsigned int rand_seed = 1;

void srand(unsigned int seed) {
rand_seed = seed;
}

int rand(void) { /*i don't understand what code do here down*/
return ((rand_seed = rand_seed * 1103515245 + 12345 ) >> 16) & 0x7fff;
}[/PHP]

I know that if we want to change the seed with time, we write [PHP]srand((int)time(NULL))[/PHP]

ps:If this thread is not in the right section please move it
Aug 8 '07 #1
4 6584
JosAH
11,448 Expert 8TB
Hello everyone,I don't speak very good english , so sorry if i make any mistake
Can anyone explain me this code
[PHP]unsigned int rand_seed = 1;

void srand(unsigned int seed) {
rand_seed = seed;
}

int rand(void) { /*i don't understand what code do here down*/
return ((rand_seed = rand_seed * 1103515245 + 12345 ) >> 16) & 0x7fff;
}[/PHP]

I know that if we want to change the seed with time, we write [PHP]srand((int)time(NULL))[/PHP]

ps:If this thread is not in the right section please move it
It's the right forum, no need to worry. You were looking at the source code for
the rand() pseudo random number generator. It's a linear congruence method.

kind regards,

Jos
Aug 8 '07 #2
kalar
82
So if understand good ,it's a method to generate random numders?
In the link that you gave me has a type about this method with X ,c and others.So in my case .We choose some numbers like 1103515245.How this choice made? maybe depends on computer?
Aug 8 '07 #3
JosAH
11,448 Expert 8TB
So if understand good ,it's a method to generate random numders?
In the link that you gave me has a type about this method with X ,c and others.So in my case .We choose some numbers like 1103515245.How this choice made? maybe depends on computer?
Yep, it generates 'pseudo random numbers'; the choice of these funny numbers
is crucial to the quality of these pseudo random numbers. Here is a nice link.
And no, those numbers are hardly related to the computer you use.

kind regards,

Jos
Aug 8 '07 #4
kalar
82
Thank you
very much
Aug 8 '07 #5