Hi,
Yesterday I replied from my parent's machine using IE that
was not cooperating with entering text, and may have not been
clear on some points. Sorry. I will make a last attempt to clean
things up now that I can edit text again in Netscape. Sigh.
In Reply #28 the ENT results were for byte streams created
from bytes taken from DWORDs created by a LCG algorithm.
One good using the high byte. One bad using the low byte.
Things seem to have gone south from there.
In Reply #32, Alex mentioned using all bytes in the DWORD
in his algorithm. I use all bytes in the LCG, but reported only
byte values in #28, so I reported DWORD ENT results in
Reply #33, noting that the results would appear bad. I should
have emphasized that the DWORDs are correlated as to the bytes
that are contained in the DWORD. That is a consequence of
the LCG algorithm and why it "is not optimum". An optimum
algorithm would not only generate decent pseudorandom values,
it would not have discernable internal structure.
brethren in Reply #34 made a comment on that comment
of mine that I tried to clarify in Reply #35 referring back to #28
and the "good" byte stream. Probably a mistake as it was too
brief?
Reply #36, #38, and #39 tried to point out the DWORD and
byte streams are different and should not be confused. Reply
#40 should have mentioned that the "byte stream" was from the
first part of Reply #28 and the DWORD stream was from #33.
MichaelW, I have had 16-bit and 32-bit assembly, plus FORTRAN
code from Numerical Recipes (and I thought my own?). All produce
the same results. Most of that is on a "dead" machine of one sort
or another. 16-bit code appended to the end, pseudo-code is;
Rand
N+1 = 1664525 * Rand
N + 1013904223
plus an initial value, plus the current time. The DWORDs, considered
as DWORDs, are a fairly good random sequence, if used properly. If
used as a stream of bytes, it is crummy. You can possibly find
a way to invalidate the use of the DWORDs, which is why more
complex algorithms are now used. For simple cases it works well.
Reply #43
Surely random is random in any dimension or vulnerable to very simple attack and not really random at all? (Note. This is a genuine question not a rhetorical question)
Right. Pseudorandom is not random. Simple is not infallible.
I was trying to show a simple algorithm to generate a decent
"random" sequence as cheaply as possible. As opposed to a
"costly approach". The simple route was muddied by the ENT
results? As I am not making sense to you (sorry), maybe "The
Art of Computer Programming", Volume 2 / Seminumerical
Algorithms, Chapter 3, Random Numbers, by Donald E. Knuth
will help. I have the second edition of 1981 and rather liked it.
Regards all,
Steve N.
DATASEG SEGMENT PUBLIC
; - - - - - -
; "Random number routine" from Knuth/Numerical Recipies
;RandLCG: ; Linear Congruential "Random" number Generator from Knuth/Numerical Recipies
Rand32 DD 31415926 ; a la K, Use zero to test NR results
RandA DD 1664525 ; As per NR
RandC DD 1013904223 ; As per NR
OutputFile DB 'RandLGC.Out', 0
DATASEG ENDS
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
CODE SEGMENT USE16 PUBLIC
ASSUME CS:CODE, DS:DATASEG, SS:STCKSEG
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
; Initialize Random Number generator...
RInit:
SCALL GTIME ; Macro to call MS-DOS Time function.
XOR Word Ptr [Rand32],DX
XOR Word Ptr [Rand32+2],CX
RET
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
; Linear Congruential "Random" number Generator from Knuth/Numerical Recipies
; IDUM = IDUM*IA + IC = MOD ( IDUM*IA + IC ), IM )
;
; Because this will run on an HP200LX, use 16 bit math and discard
; the high order results.
; 30 January 2001, SRN
; 8 July 2004, tidy up... [Rand32+0]*[RandA+0] => [I1][LOR]
; [Rand32+0]*[RandA+2] => [I3][I2]
; [Rand32+2]*[RandA+2] => [I5][I4]
; 9 July 2004, conform to Numerical Recipes values
RandLCG:
; Multiply part
MOV AX,WORD PTR [Rand32]
MOV BX,AX
MUL WORD PTR [RandA]; Low order words => DX = I1, AX = LOR
PUSH AX ; save low order result
MOV CX,DX ; save intermediate result 1
MOV AX,BX
MUL WORD PTR [RandA+2]; Low & Medium words => DX = I3, AX = I2
MOV BX,AX ; save intermediate result 2
MOV AX,WORD PTR [Rand32+2]
MUL WORD PTR [RandA]; Medium & Low order words => DX = I5, AX = I4
; Accumulate part
ADD BX,AX ; I2 + I4
ADD BX,CX ; I2 + I4 + I1 ; 9 July 2004 (was ADC)
POP AX ; LOR
; Add part
ADD AX,WORD PTR [RandC]
ADC BX,WORD PTR [RandC+2]
MOV WORD PTR [Rand32],AX
MOV WORD PTR [Rand32+2],BX
RET
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
CODE ENDS