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

rand function

P: n/a
what algorithm does the rand function use?

Apr 22 '06 #1
Share this Question
Share on Google+
3 Replies

P: n/a
bo* wrote:
what algorithm does the rand function use?

Read the man, info or whatever page and find out!

Ian Collins.
Apr 22 '06 #2

P: n/a

<bo*> wrote in message
what algorithm does the rand function use?

The algorithm isn't specified in the standard (the C standard in this case).
Note that it is only required to deliver 16 bits. See the value of RAND_MAX
for your compiler.
Apr 23 '06 #3

P: n/a
<bo*> wrote:
what algorithm does the rand function use?

As mentioned elsewhere, whatever the deigner chooses to use.

The link below suggests:

static unsigned long int next = 1;

int rand(void) // RAND_MAX assumed to be 32767
next = next * 1103515245 + 12345;
return (unsigned int)(next/65536) % 32768;

is at least acceptable. If one wanted a quick and dirty generator with
uniform results across different platforms, one might use that. Calling
rand() will likely give different results with different compilers. I
recall seeing that 12345 in a snippet elsewhere on the net, too.
Apr 23 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.