about rand

X

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?
 
A

A. Sinan Unur

xcm said:
#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?
 
P

Peter Nilsson

xcm wrote:
next = next * 1103515245 + 12345;
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top