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

about rand

P: n/a
xcm


#include <stdlib.h>

static unsigned long int next = 1;

int rand(void)
{
next = next * 1103515245 + 12345;
return (unsigned int)(next/(2 * (RAND_MAX +1L)) % (RAND_MAX+1L));
}

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


next = next * 1103515245 + 12345;
1103515245,12345
why not 1103511234 ,54321?

Nov 15 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"xcm" <ku****@163.com> wrote in news:dj**********@news.yaako.com:
#include <stdlib.h>

static unsigned long int next = 1;

int rand(void)
{
next = next * 1103515245 + 12345;
return (unsigned int)(next/(2 * (RAND_MAX +1L)) % (RAND_MAX+1L));
}

void srand(unsigned int seed)
{
next = seed;
}
rand and srand are functions provided by the standard library. Why are
you re-writing them?
next = next * 1103515245 + 12345;
1103515245,12345
why not 1103511234 ,54321?


What do you mean?

--
A. Sinan Unur <1u**@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)
Nov 15 '05 #2

P: n/a
xcm wrote:
<snip>
next = next * 1103515245 + 12345; <snip>
1103515245,12345
why not 1103511234 ,54321?


Well, why not 42 and 27?

Do some googling on PRNG and linear congruential generators.

Your question doesn't seem to be about C specifically, rather it
seems to be about a particular algorithm used to implement one
version of rand(). But the C standards don't require a specific
algorithm for rand(), and comp.lan.c is not an algorithms group.

--
Peter

Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.