answersLogoWhite

0


Best Answer

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

βˆ™ 14y ago
This answer is:
User Avatar

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
Continue Learning about Engineering
Related questions

Getting source code of c?

C is a programming language, so it doesn't have source code.(On the other hand, C compilers do have source code, but you, as a beginner in programming, could not understand them.)


How do you Open a text file as C plus plus source code?

All C++ source code is is a text file with the .cpp extension. So if you save your code as *****.cpp then it is automatically C++ source code.


What is the source code for scientific calculator in c?

What is the source code for scientific calculator in c?Read more: What_is_the_source_code_for_scientific_calculator_in_c


Stack overflow Exception source code in c sharp?

You have to ask Microsoft for the source code


How do you convert .c extension to .exe?

To convert source code (.c file) to an executable (.exe) file you have to use a compiler, which is a translator of source code to machine code.


What is the differentiate of turbo c from turbo c plus plus?

Turbo C compiles c source. turbo c++ compiles c++ source code.


Where do you get the exact source code in C that implements lossless compression algorithm?

The source code, in C, will depend on what type of lossless compression algorithm will be used. A source code should be available from various computer scientists in your area.


What type of a program is used in order to enter c source code?

What type of a program is used in order to enter C source code


How do you convert c exe to source code?

Not possible.


What is a source code file in c plus plus?

A source code file is a plain-text file containing C++ instructions. The instructions must be compiled and linked to create a native machine code executable.


What is meaning of PHP is an open source software?

It means that the source code of PHP (not PHP code, but the C/C++ code used to build the PHP binary) is published and available to anyone. It also means that anyone can contribute fixes and improvements to the source code.Read more here: open-source


Source code of lotto game lottery in c?

sravanthi