News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

New method to generate random numbers

Started by ToutEnMasm, August 23, 2005, 07:55:08 PM

Previous topic - Next topic

ToutEnMasm

Hello,
How it work ?
Two limits must be defined for the number,a min and a max
I use two things, a timer and a thread .The thread only loop inside all possibles values of eax and only windows know how many cycles it does during the time.
it works perfectly and is disturbed only when windows works very hard.
         ToutEnMasm

; #########################################################################
.386
.model flat, stdcall
option casemap :none   ; case sensitive

; #########################################################################

include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
include  \MASM32\include\masm32.inc

includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib

;include macro.inc
ThreadRandom PROTO :DWORD
WndProc PROTO  :DWORD,  :DWORD,  :DWORD,  :DWORD
RandomProc PROTO
winmain PROTO


STHREAD STRUCT
Htread dd ?
threadid dd ?
STHREAD ENDS
;-------------------------------------- macros ------------------------------------------------
ZEROLOCALES MACRO dernierelocale:REQ
mov ecx,ebp
lea edx,dernierelocale
sub ecx,edx
.if ecx != 0
push edi
mov edi,edx
mov al,0
cld
rep stosb
pop edi
.endif
ENDM
PuPo MACRO M1, M2
push M2
pop  M1
ENDM

;---------------------------------------------------------------------------------------------

.data
Tcompile STHREAD <>
szClassName db "invisible",0
ThreadTerminate dd 0
MiniMum dd 0
Maximum dd 0
hWnd dd 0
random dd 0
ZoneMessagesErreurs db 500 dup (0)
asciirandom db 20 dup (0)
espace db " ",0
CPT dd 0
szrandom db "random",0
.code
start:
;--------- define the limits ------------------------
mov MiniMum,0
mov Maximum,1000
mov CPT,20 ;numbers of random to generate
;---------------------------------------------------------
randomize:
invoke winmain
invoke dwtoa,random,addr asciirandom
invoke szCatStr,addr ZoneMessagesErreurs ,addr asciirandom
invoke szCatStr,addr ZoneMessagesErreurs ,addr espace
dec CPT
.if CPT != 0
jmp randomize
.endif
;--------------- view the result -------------------------------
invoke MessageBox,NULL,ADDR ZoneMessagesErreurs,addr szrandom,MB_OK
invoke ExitProcess,0
;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
winmain PROC
LOCAL wc   :WNDCLASSEX
LOCAL msg  :MSG
LOCAL Hinstance:DWORD
ZEROLOCALES Hinstance

invoke GetModuleHandle,NULL
mov Hinstance,eax

;==================================================
; create a not visible window
;==================================================

mov wc.cbSize,         sizeof WNDCLASSEX
mov wc.style,          CS_HREDRAW or CS_VREDRAW
mov wc.lpfnWndProc,    offset WndProc       
PuPo wc.hInstance,      Hinstance   
mov wc.hbrBackground,  COLOR_BTNFACE+1
mov wc.lpszClassName,  offset szClassName

invoke RegisterClassEx, ADDR wc
invoke CreateWindowEx,0,ADDR szClassName,0,0,0,0,0,0,0,0,Hinstance,0
;-------------- create a thread -------------------------------------------
invoke CreateThread,NULL,NULL,ThreadRandom,NULL,NULL,addr Tcompile.threadid
mov Tcompile.Htread,eax
;pass time
invoke SetTimer,hWnd,400,1,NULL ;wait one millisecond and put the test to 1

StartLoop:
invoke GetMessage,ADDR msg,NULL,0,0 ;wait for time finished
.if eax != 0
.if eax != -1 ;invalid handle
invoke TranslateMessage, ADDR msg
invoke DispatchMessage,  ADDR msg
.endif
jmp StartLoop
.endif
   ;------------ modify not in the zip ---------------------------------
invoke UnregisterClass,addr wc,Hinstance
invoke KillTimer,hWnd,400
ret
winmain endp

;################################################################

ThreadRandom PROC Nothing:DWORD
mov eax,MiniMum
@@:
.if ThreadTerminate != 1 ;is time paste ? ,NOT
.if eax != Maximum ;loop inside all possibles values of eax
inc eax ;
jmp @B
.else
mov eax,MiniMum
jmp @B
.endif
.endif
mov random,eax
invoke SendMessage,hWnd,WM_CLOSE,NULL,NULL ; close the window
invoke CloseHandle,Tcompile.Htread ;close the thread
mov ThreadTerminate,0 ; for the next number
mov Tcompile.Htread,0
mov Tcompile.threadid,0
ret
ThreadRandom endp
;----------------------------------------------------------------------
;################################################################
WndProc proc  hwnd:DWORD, wMsg, wParam, lParam
mov eax,hwnd
mov hWnd,eax
.if wMsg == WM_TIMER
mov ThreadTerminate,1 ;stop the thread
xor eax,eax ; valeur de retour
.elseif wMsg == WM_CLOSE || wMsg == WM_DESTROY
invoke PostQuitMessage,NULL
mov eax,0
.else
invoke DefWindowProc,hwnd, wMsg, wParam, lParam
.endif
ret
WndProc endp
end start

[attachment deleted by admin]

Codewarp

If I called this method for 1000 random numbers, real fast, what kind of numerical distribution can you guarantee from this work?  I ask, because it looks like it may be somewhat skewed. 

ToutEnMasm

Hello,
I can grant you that the buffer is too small for 1000 iterations.
It is put here for serious testing,made it, and perhaps will you find something to make it better.
That i can say,is that i have generated  about 300 numbers and that I have not find any problems others that written in my first mail.
If you have difficulties to see how it work,i can add this:
it's a wheel turning always at the same speed but never the same time.
If you want to make 1000 ierations ,delete the buffer and use writetomemoryhelp a new function that i put here.
                    ToutEnMasm

                                                                 

hutch--

ToutEnMasm,

For tsting random sequences, its hard to beat John Walkers ENT testing program, its on te forum web site.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

ToutEnMasm

Hello,
I generated 1000 randoms numbers separated by space in a file.
The range for the numbers is 0 to 1000
I have  used ent without options and the result follow
I don't see why ent find 3880 samples ?
Two  lines must be added to the source,before the end of winmain and i  modified my primary source mail.

invoke UnregisterClass,addr wc,Hinstance
invoke KillTimer,hWnd,400

                        TouEnMasm


C:\tmp>ent random.txt
Entropy = 3.276413 bits per byte.

Optimum compression would reduce the size
of this 3880 byte file by 59 percent.

Chi square distribution for 3880 samples is 118008.73, and randomly
would exceed this value 0.01 percent of the times.

Arithmetic mean value of data bytes is 47.3624 (127.5 = random).
Monte Carlo value for Pi is 4.000000000 (error 27.32 percent).
Serial correlation coefficient is -0.329243 (totally uncorrelated = 0.0).

ToutEnMasm

To hutch,
ENT seems to know only bytes , not decimal ascii numbers and i made another file with
********** range  0 to 255
********** 256 numbers generated
the result follow
Is there a possibility to test decimal ascii numbers ?
                                          ToutEnMasm



C:\tmp>ent random.txt
Entropy = 7.177568 bits per byte.

Optimum compression would reduce the size
of this 256 byte file by 10 percent.

Chi square distribution for 256 samples is 278.00, and randomly
would exceed this value 25.00 percent of the times.

Arithmetic mean value of data bytes is 123.7305 (127.5 = random).
Monte Carlo value for Pi is 2.952380952 (error 6.02 percent).
Serial correlation coefficient is -0.101151 (totally uncorrelated = 0.0).

MichaelW

I modified the code to generate 300 numbers in the range 0-9 and display the frequency of the generated numbers. Here is the output for 3 successive runs:

73 35 18 28 32 26 15 33 16 24
96 30 21 20 26 21 18 30 15 23
95 31 15 33 22 21 19 22 12 30

The minimum value occurs at a frequency 2-3 times higher than the other values.




[attachment deleted by admin]
eschew obfuscation

ToutEnMasm

Hello,
I have made littles modifies to break all kinds of frequency.
I have added a first random choice with the counter of cycles,RDTSC,Numbers are inc or dec .
The price is the .586 instead of .386
I have added also a file to write the numbers,it's not very fast , several seconds are needed to have a thousand of random numbers.The file is named random.txt
This time, the memory buffer accept any numbers of randoms,it calculate the memory needed.
A messagebox prompt you when it is finished
I join the compressed file.

                                     ToutEnMasm



[attachment deleted by admin]

Mark Jones

Hi MasmTut, what results do you get from ENT.exe?
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08