View Full Version : Random Number Generator in GLSL

06-16-2009, 02:43 PM
Hello All,

I want to write a shader that generates random numbers based on Gaussian Distribution in range [-X, X] or [0, X]. I thought of using noise*() where, * = 1,2,3,4, but unfortunately came to know, through this forum, that they are not supported on NVIDIA Cards.
(I have GeForce 8800GS).

Then I thought of writing my own code using rand(). I am doubtful whether is there a rand() in GLSL as in C programming?

Is there any other good suggestion to generate normally distribute random number generator without using rand()?

Guidance Appreciated.

Lord crc
06-16-2009, 04:34 PM
Do you want it to be accurate or fast? For fast, you could generate "random" numbers using the method described here: http://lumina.sourceforge.net/Tutorials/Noise.html

You could then transform the random numbers to a Gaussian distribution by using the Gaussian CDF (http://en.wikipedia.org/wiki/Normal_distribution#Cumulative_distribution_functi on). Store the CDF in a 1D texture (x = -5..5 or -6..6 should do), then use the random number as texcoord. Or approximate it some other way :)

06-16-2009, 06:37 PM
Thank you Lord_crc for replying. I went to the link pointed to by you. I have not tried their method but hope to get results.

......and yes, I can a bit compromise on accuracy.

Lord crc
06-16-2009, 07:05 PM
Sorry, I was a bit quick, you want to use the inverse CDF. It is given in the same section as the link I gave above. So generate numbers in 0..1 and look up the inverse CDF.

An approximation of the inverse error function may be handy:http://homepages.physik.uni-muenchen.de/~Winitzki/erf-approx.pdf