flat assembler
Message board for the users of flat assembler.
![]() |
Author |
|
sina 06 Nov 2003, 17:54
how can i make a random number between 0 and 49 an then i will assign it to a dialog box control like a static text
|
|||
![]() |
|
roticv 07 Nov 2003, 02:20
In masm code:
Code: ; ################################################## ####################### ; ; Park Miller random number algorithm written by Jaymeson Trudgen (NaN). ; ; ################################################## ####################### nrandom PROC base:DWORD mov eax, nrandom_seed xor edx,edx push 127773 div DWORD PTR [esp] push eax mov eax, 16807 mul edx pop edx push eax mov eax, 2836 mul edx pop edx sub edx, eax mov eax, edx mov nrandom_seed, edx push base mov edx, 0 div DWORD PTR [esp] add esp,8 mov eax, edx ret nrandom ENDP |
|||
![]() |
|
comrade 08 Nov 2003, 02:02
just go GetTickCount() mod 50
|
|||
![]() |
|
Madis731 08 Nov 2003, 08:13
You can only ask GetTickCount() some time.
You can not do it 10000 times per second, or your numbers won't be random at all, but still Comrade here, had the simplest idea & these are always the best:) |
|||
![]() |
|
sina 14 Nov 2003, 01:00
i am sorry i didnot have time to say thanx for all your helpfull efforts
thanks so much.. |
|||
![]() |
|
vid 14 Nov 2003, 08:10
instead of GetTickCount you can use RDTSC instruction. It sets edx:eax to number of processor clocks from turning on the computer. It exists from pentium. If you want better results multiply it with something.
|
|||
![]() |
|
scientica 14 Nov 2003, 10:18
IIRC GetTickCount is basically rdtsc, but in the Win API method (for HLL programmers that dont know asm, let alone anything 'bout the rdtsc instruciton).
I most cases GetTickCount will do fine, even if you call in after each other you might get a bigger delta then expected , the scheduled might for instance have put you app on a hold because some other task needed to be executed - the thing is you never know how long time it'll take t make two subsequent calls to an API. If you want to make some more pseudoRandom you can do it like this: Code: ; -- u n t e s t e d -- ; data sec: Seed dd 0xF865ABED ; code sec proc pseudoRand enter call [GetTickcount] mov edx, 5 ; small digit - unless we ant to risc waiting a long time... xor eax, [Seed] idiv edx ; edx = remainder mov [Seed], eax invoke Sleep, edx call [GetTickcount] mov edx, 50 ; put the maximum wanted digit (MWD) + 1 here add [Seed], eax div edx mov eax, edx ; edx is now a pseudoRandom digit, in the range 0 <-> MWD. return _________________ ... a professor saying: "use this proprietary software to learn computer science" is the same as English professor handing you a copy of Shakespeare and saying: "use this book to learn Shakespeare without opening the book itself. - Bradley Kuhn |
|||
![]() |
|
Madis731 14 Nov 2003, 21:41
Are we talking ablut the same GetTickCount() ?
I know that API function refreses this timer about every 0,0009765625 seconds, but you are talking, like you can call it hundreds of thousands of times per second. I'm a bit confused, because the tick would be the same for the whole millisecond ![]() rdtsc is good, but can it be the same thing with GetTickCount? |
|||
![]() |
|
scientica 14 Nov 2003, 21:49
My misstake: (from win32.hlp):
Quote: The GetTickCount function retrieves the number of milliseconds that have elapsed since Windows was started. But I guess this mean we'll be able to remove these lines in my proc above, since it'll never get large: mov edx, 5 ; small digit - unless we ant to risc waiting a long time... ;... idiv edx ; edx = remainder ![]() ![]() _________________ ... a professor saying: "use this proprietary software to learn computer science" is the same as English professor handing you a copy of Shakespeare and saying: "use this book to learn Shakespeare without opening the book itself. - Bradley Kuhn |
|||
![]() |
|
sina 13 Dec 2003, 01:44
so i have somethign like this which does not work
becouse i could not use your proc that just crashed Code: format PE GUI 4.0 entry start include '%include%\win32a.inc' ID_Close = 101 ID_Guess = 102 ID_Label1 = 103 section '.data' data readable writeable flags dd ? temp1 dd ? section '.code' code readable executable start: invoke GetModuleHandle,0 invoke DialogBoxParam,eax,37,HWND_DESKTOP,DialogProc,0 or eax,eax jz exit exit: invoke ExitProcess,0 proc DialogProc,hwnddlg,msg,wparam,lparam enter push ebx esi edi cmp [msg],WM_COMMAND je wmcommand cmp [msg],WM_CLOSE je wmclose xor eax,eax jmp finish wmcommand: cmp [wparam],ID_Close je wmclose cmp [wparam],ID_Guess je guess guess: call Rand mov [temp1],eax invoke SetDlgItemText,[hwnddlg],ID_Label1,temp1 jmp processed wmclose: invoke EndDialog,[hwnddlg],0 processed: mov eax,1 finish: pop edi esi ebx return proc Rand enter call [GetTickCount] return section '.idata' import data readable writeable library kernel,'KERNEL32.DLL',\ user,'USER32.DLL' import kernel,\ GetModuleHandle,'GetModuleHandleA',\ Sleep,'Sleep',\ GetTickCount,'GetTickCount',\ ExitProcess,'ExitProcess' import user,\ DialogBoxParam,'DialogBoxParamA',\ SetDlgItemText,'SetDlgItemTextA',\ EndDialog,'EndDialog' section '.rsrc' resource data readable directory RT_DIALOG,dialogs resource dialogs,\ 37,LANG_ENGLISH+SUBLANG_DEFAULT,lotodialog dialog lotodialog,'Loto Guess with FASM',70,70,190,175,WS_CAPTION+WS_POPUP+WS_SYSMENU+DS_MODALFRAME dialogitem 'STATIC','Numbers:',ID_Label1,10,10,70,8,WS_VISIBLE dialogitem 'BUTTON','Guess',ID_Guess,85,150,45,15,WS_VISIBLE+WS_TABSTOP+BS_DEFPUSHBUTTON dialogitem 'BUTTON','Close',ID_Close,135,150,45,15,WS_VISIBLE+WS_TABSTOP+BS_PUSHBUTTON enddialog which produces meaningless characters in the label |
|||
![]() |
|
aaro 13 Dec 2003, 16:35
Change SetDlgItemText to SetDlgItemInt
|
|||
![]() |
|
sina 13 Dec 2003, 21:55
and scientica the code u have posted crashes can u help me here
|
|||
![]() |
|
JohnFound 13 Dec 2003, 22:03
Well, I don't like using GetTickCount or RDTSC for random number generator.
GetTickCount is not a random generator. It is a clock. If you look it in random moments you will get random numbers, if you look it in regular moments you will get regular numbers. ![]() |
|||
![]() |
|
sina 13 Dec 2003, 22:58
thanks roticv aaro scientica and johnfound
so this is the code i used Code: proc nrandom , base enter mov eax,[seed] xor edx,edx push 127773 div dword [esp] push eax mov eax, 16807 mul edx pop edx push eax mov eax, 2836 mul edx pop edx sub edx, eax mov eax, edx mov [seed], edx push [base] mov edx, 0 div dword [esp] add esp,8 mov eax, edx return i have added Code: invoke GetTickCount mov [seed],eax line in the start after enter becouse in every restart of the program it produced the same numbers in the same order |
|||
![]() |
|
roticv 14 Dec 2003, 05:20
Why not replace GetSystemTick with
Code:
rdstc
xor eax, edx
|
|||
![]() |
|
sina 14 Dec 2003, 13:28
yes that worked out great
thanks |
|||
![]() |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.