hey everyone, i thought i would post this rand i recently wrote because i was having troubles finding one that fit my needs.
any tips on optimization would be appreciated


mov esi, offset buffer        ;buffer for rand output
mov edi, 500000d              ;size in bytes
mov ebx, 49282048h            ;seed
add edi, esi
xor ecx, ecx

@@:
rdtsc
rol eax, 5
lea eax,
shr eax, 3
ror edx, 3
lea edx,
rol edx, 7
or eax, edx
add eax, esi
add cl, al
ror cl, 3
rol eax, cl
xor cl, al
add cl, ah
rol eax, cl
lea eax,
xor eax, ebx
mov cl, bh
mov ch, bl
ror cx, cl
rol ax, cl
ror eax, 15
lea eax,
and ecx, ebx
ror eax, cl
mov , eax
rdtsc
mov ebx, eax
lea ebx,
lea ecx,
rol ebx, cl
lea ebx,
ror cx, cl
ror ebx, cl
add esi, 4
cmp edi, esi
jl @B


1gb of data generated by this rand, tested with ent:

Entropy = 8.000000 bits per byte.

Optimum compression would reduce the size
of this 1073741824 byte file by 0 percent.

Chi square distribution for 1073741824 samples is 268.19, and randomly
would exceed this value 50.00 percent of the times.

Arithmetic mean value of data bytes is 127.4989 (127.5 = random).
Monte Carlo value for Pi is 3.141590272 (error 0.00 percent).
Serial correlation coefficient is 0.000092 (totally uncorrelated = 0.0).
Posted on 2010-01-27 12:53:20 by paradox
Looks like a simple variation of Park-Miller, which is not a very good random generator.
Using spatial analysis of the numeric keyspace, I determined that Park-Miller generates 'clusters' of values, ie, does not have a very good 'spread' across the numerical domain.
My response was to take Mersenne Twister (B) and Park-Miller, and introduce feedback between the two algorithms. I have not discovered a RNG which has a better 'spread' than this combination, and that was some years ago now.
I am not convinced that Chi-Square is a good measure of numerical distribution of a RNG.
N-Dimensional visualisation of the raw output is perhaps a better gauge.
Except for the rdtsc instruction, I would recommend MMX optimizing.
What is your intended purpose? That's the crucial question.
Posted on 2010-01-28 02:11:39 by Homer
homer, i originally wrote it to generate a set of 1gb files so i could compare the speed of a few sorting algorithms
Posted on 2010-01-28 06:23:01 by paradox
For me, I wanted a RNG which was guaranteed to produce the same numerical sequence , given the same initial seed, as my purpose was a 4096-bit custom block cypher for 8 bit data based loosely on Vignere's cypher.
The rdtsc opcode is somewhat unreliable without a wait, but it is not unpredictable.
Your output receives regular 'jitter' from the use of this opcode which makes the output appear more random than it actually is... in effect, your algorithm is more complex than it first appears to be, which is good!
Posted on 2010-01-28 06:39:25 by Homer