Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random number generation without using bit operations

I'm writing a vertex shader at the moment, and I need some random numbers. Vertex shader hardware doesn't have logical/bit operations, so I cannot implement any of the standard random number generators. Is it possible to make a random number generator using only standard arithmetic? the randomness doesn't have to be particularly good!

like image 981
Martin Avatar asked Jun 17 '09 22:06

Martin


2 Answers

If you don't mind crappy randomness, a classic method is

x[n+1] = (x[n] * x[n] + C) mod N

where C and N are constants, C != 0 and C != -2, and N is prime. This is a typical pseudorandom generator for Pollard Rho factoring. Try C = 1 and N = 8051, those work ok.

like image 142
rlbond Avatar answered Oct 19 '22 08:10

rlbond


Vertex shaders sometimes have built-in noise generators for you to use, such as cg's noise() function.

like image 22
Alex Brown Avatar answered Oct 19 '22 10:10

Alex Brown