By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,982 Members | 1,738 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,982 IT Pros & Developers. It's quick & easy.

rand seed in C (explain a code)

P: 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
Thanks in advance
Aug 8 '07 #1
Share this Question
Share on Google+
4 Replies


Expert 10K+
P: 11,448
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
Thanks in advance
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.
Read all about that method here.

kind regards,

Jos
Aug 8 '07 #2

P: 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

Expert 10K+
P: 11,448
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

P: 82
Thank you
very much
Aug 8 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.