Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random numbers with arbitrary probability distribution using rand() only

I am looking for a simple algorithm how to generate pseudo-random floating point numbers using only ANSI rand() function but with arbitrary probability distribution. For a simple uniform distribution I use following code:

x = (float)rand() / (float)RAND_MAX;

Of course it is not very accurate, but enough for my needs. I need also other distributions like logistic and gaussian. Ideally I have to define an arbitrary pdf using a simple vector of finite length, e.g. for logistic pdf this vector may look like:

logistic_pdf = {0., 0.26894, 0.33924, 0.41742, 0.5, 0.58257, 0.66075, 1.};

and for uniform (using same dimensionality 8):

uniform_pdf = {0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125};

This is only an idea. But I am not sure how to implement it efficiently using rand()->{0...RAND_MAX} only.

like image 265
psihodelia Avatar asked Nov 30 '12 12:11

psihodelia


People also ask

What is an arbitrary probability distribution?

An arbitrary distribution, whose values and probabilities are user specified. A Gaussian (or Normal) distribution with a specified mean and standard deviation. A uniform (or flat) distribution, with events equally likely to occur anywhere within the interval from 0 to 1.

How do I generate random numbers without rand () function?

For "not too random" integers, you could start with the current UNIX time, then use the recursive formula r = ((r * 7621) + 1) % 32768; . The nth random integer between 0 (inclusive) and M (exclusive) would be r % M after the nth iteration. This is called a linear congruential generator.

How do you sample an arbitrary probability distribution given the probability density?

Sampling from an arbitrary distribution can be done by sampling uniformly in [0, 1] then using the inverse of the cumulative density function.


1 Answers

there is no simple algorithm to do arbitrary complex things. you have to find the inverse probability integral transform for each of your 'arbirary' distributions.

like image 107
stefan Avatar answered Nov 14 '22 22:11

stefan