471,319 Members | 1,770 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

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
Thanks in advance
Aug 8 '07 #1
4 6378
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
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
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

Post your reply

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

Similar topics

3 posts views Thread by HUNGER | last post: by
7 posts views Thread by Chris Gordon-Smith | last post: by
36 posts views Thread by Ben Justice | last post: by
36 posts views Thread by Profetas | last post: by
reply views Thread by Skybuck Flying | last post: by
11 posts views Thread by Fernando Barsoba | last post: by
10 posts views Thread by Frank Silvermann | last post: by
5 posts views Thread by ds | last post: by
15 posts views Thread by Rich Fife | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.