Random number generation without using bit operations

I am writing a vertex shader at the moment, and I need some random numbers. The Vertex shader hardware does not have logical / bit operations, so I cannot implement any of the standard random number generators. Is it possible to create a random number generator using only standard arithmetic? chance should not be particularly good!

+6
math random graphics
source share
4 answers

If you don't mind crappy chance, the classic method

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

where C and N are constants, C! = 0 and C! = -2, and N is a prime. This is a typical pseudo-random generator for factoring Pollard Roe. Try C = 1 and N = 8051, they work fine.

+5
source share

Vertex shaders sometimes have built-in noise generators that you can use, for example, cg noise() .

+2
source share

Use a linear congruent generator :

 X_(n+1) = (a * X_n + c) mod m 

Those are not so strong, but at least they are well known and can have long periods. The Wikipedia page also has some good recommendations:

The total LCG period is not more than m, and for some options it is much less than that. An LCG will have a full period if and only if:

 1. c and m are relatively prime, 2. a - 1 is divisible by all prime factors of m, 3. a - 1 is a multiple of 4 if m is a multiple of 4 
+2
source share

Believe it or not, I used newx = oldx * 5 + 1 (or a slight change) in several video games. Accident is terrible - it is a scrambled sequence rather than a random generator. But sometimes that's all you need. If I remember correctly, he looks through all the numbers before repeating.

He has some terrible characteristics. He never gives you the same number twice in a row. Some of us did a bunch of variation tests, and we used some variations in other games.

We used it when we did not have a good module. This is just a shift by two and two additions (or multiplication by 5 and one addition). I would never use it at present for random numbers - I use LCG - but it may work fine for a shader where speed is critical and your instruction set may be limited.

+2
source share

All Articles