Submitted by , posted on 15 July 2002
Image Description, by
Whats all that noise about!?
After reading a thread on random number I wanted to show how simplistic a random function can get.
There is some beauty and simplicity hidden in all this noise.
Here on the left we see the ANSI rand() function applied to RGB pixel (320x240),
on the right we see an alternative Rnd() function.
Below each image is their respective averaged color (127,127,127).
Its not possible to really see a seam between the two functions so anyone content with the rand() function will find this
alternative algorithm applicable and possibly over 100 time faster.
Here is how:
rand() basically execute an average of 54 instructions for 16bit including instruction like multiplication,
in contrast Rnd() execute only this for 64bit:
pshufw mm1, mm0, 0x1E
paddd mm0, mm1
(Where mm0 is the 'melange'. mm1 is just a temp register.)
In detail;In the first instruction we shuffle 16bit chunks from this order A:B:C:D to D:C:A:B .
Other shuffle work equality well. 0x4B for example.
We end up doing this mix operation : AB + DC : CD + AB
Using SSE2 its possible to get 4 floating point random value in a total of 4 instructions.
Fast enough to consider for heavy realtime usage.
The function can be fed simple seeds, like one (mm0 = 1) and after a some iteration mm0 will start to show random behaviour.
Here is how the sequence start:
01 00 00 00 00 00 01 00 01 00 01 00 00 00 02 00
01 00 03 00 01 00 03 00 02 00 06 00 04 00 04 00
... And later on complicates itself to this ...
21 EC 0D 12 62 9E CC 5F 83 8A DA 71 6F B0 ED 4B
F2 3A C8 BD 49 22 71 D6 3B 5D 39 94 11 E0 63 11
What look like chaos to some is considered complex by other, yet is simple in nature.
Stephan Schaem (a.k.a T21)