answersLogoWhite

0

The source code for one version of rand(), producing a uniform pseudo-random distribution, is ...

int rand() {

static unsigned seed = 0;

seed = seed * 0x343FD + 0x269EC3;

return seed >> 16 & 0x7FFF;

}

See below for the original answer, including this piece of code...

A uniform distribution is a sequence of events or observations that have equal, i.e. uniform distribution across their probability domain. For example, a fair six sided die has probability 0.167 of having each face show up on a roll. It is, over the long term, uniformly distributed across the discrete probability domain [1,2,3,4,5,6].

As a comparative example, two dice have a triangular distribution for their sum, said sum being in the interval [2-36], with 12 having probability 0.167, 2 and 36 having probability 0.0556, 3 and 37 having probability 0.08333, etc.

There are other distributions, such as gaussian and poisson, but the question asked about uniform, so lets go back there.

You are talking about a random number generator with a uniform, i.e. equal distribution over a certain interval. One way to do this is with a pseudo-random number generator in the run-time library called rand(). When rand() is invoked, it returns an int in the interval [0-RAND_MAX] that is reasonably uniform and random. Each time it is invoked, it returns a different value, although, after a while, the sequence repeats. The sequence of values is always the same from program run to run, but it can be initialized to another sequence by invoking srand(int) to set a new seed, such as based on the clock.

I say reasonably uniform because rand() is usually based on a linear congruential generator, which is very simple, but has defects due to sequential correlation and, if its parameters are not chosen well, limitations on range and spectral purity. Also, it might not be construed as truly random.

Randomness, however, is in the "eye of the beholder", and rand() is perfectly adequate for most applications involving gaming and basic statistical analysis. It can certainly be improved, and it can be replaced.

The source code for one version of rand() is ...

int rand() {

static unsigned seed = 0;

seed = seed * 0x343FD + 0x269EC3;

return seed >> 16 & 0x7FFF;

}

This version is not thread safe. A more practical version would maintain either a per-thread copy of the seed, or pass the address of the seed, said seed being maintained by the caller, but this example serves to show the basic algorithm.

This is a linear congruential generator based on a 32 bit unsigned seed. It has a period of 4,294,967,296, which is maximal for a 32 bit value. Only 15 bits, however, are returned, the 2nd through the 16th, so that sequential correlation issues are minimized. Over the long run, specifically 4,294,967,296 iterations, each possible value will be repeated exactly 8,589,934,591 times, in what appears to be a random sequence. It is, thus, a uniform distribution, over the interval [0-32767].

If you wanted a different interval, such as the simulation of rolling a die, you can convert to a floating interval [0-1], multiply by a number, and truncate. A die version could be ...

int die() {

return int ((double) rand() / RAND_MAX * 6) + 1;

}

This returns a uniform integer in the interval [1, 6];

User Avatar

Wiki User

15y ago

Still curious? Ask our experts.

Chat with our AI personalities

BlakeBlake
As your older brother, I've been where you are—maybe not exactly, but close enough.
Chat with Blake
LaoLao
The path is yours to walk; I am only here to hold up a mirror.
Chat with Lao
ProfessorProfessor
I will give you the most educated answer.
Chat with Professor

Add your answer:

Earn +20 pts
Q: What is the C source code for uniform distribution?
Write your answer...
Submit
Still have questions?
magnify glass
imp