An interesting subject. Suitable for fun applications and subject to many technical considerations.
I've used several pseudo-random number generators. Linear congruenntial is the best: random, simple and fast. Greg developed this version many years ago and did exhaustive testing.
I've generated random dots on the screen and displayed histograms without seeing any patterns.
This version of multiply and add is unique to this application. Seed is on the stack and the low-order of the product is replaces it. The high-order is returned as random
- *+ nam-nn a! 17 for +* unext push drop a pop ;
These decimal numbers have good properties:
This generates 18 random bits. Any number can be used as a seed to start generating successive random numbers, including 0.
These pseudo-random numbers are said to have a period of only 65536. So avoid cycles with power-of-2 repetitions.
Typically you would mask fewer than 18 bits. For display using run-length video: ran 1fff and.
If you want a range that's not a power of 2:
I'm not persuaded the statistics are as good as with a power of 2, but that's the best I know to do.
- clip nn-nn 1ffff and 100 *. ;
- Used as ran clip
Summing 4 uniform numbers yields a normally distributed number. It's a severe test of randomness. I saw it develop on a 256-point histogram, summing 64-bit numbers. Magical.
The range is 4 times the uniform range with the peak centered. The range looks to be 3 sigma.
- norm n-nn ran 2 for push ran pop . + next ;
17-bit fractional multiply, discarding multiplicand
- b0 org *.17 0 org *. nf-n *.17 push drop pop ;