flat assembler
Message board for the users of flat assembler.

Index > Windows > how to make a random number

Author
Thread Post new topic Reply to topic
sina



Joined: 18 Aug 2003
Posts: 132
Location: istanbul turkey
sina
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
Post 06 Nov 2003, 17:54
View user's profile Send private message ICQ Number Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2140
Location: Estonia
Madis731
It would be easier with powers of 2, but ok:
I would use eax & add/mul/sub/div with some prime constants letting over/underflows occur. You'll get some random 32bit numbers.
Next power of 2 from 49 would be 64 so with a 111111 bitmask, you'll get a number from 0 to 63. Now you subtract 14. You'll get a carry(or underflow so-to-speak) at least some point. Then you need to try again.

I think you got the point Smile
If you didn't you can always ask for more...
Post 06 Nov 2003, 21:48
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
roticv



Joined: 19 Jun 2003
Posts: 374
Location: Singapore
roticv
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

    
Post 07 Nov 2003, 02:20
View user's profile Send private message Visit poster's website MSN Messenger Reply with quote
comrade



Joined: 16 Jun 2003
Posts: 1137
Location: Russian Federation
comrade
just go GetTickCount() mod 50

_________________
comrade (comrade64@live.com; http://comrade.ownz.com/)
Post 08 Nov 2003, 02:02
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2140
Location: Estonia
Madis731
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:)
Post 08 Nov 2003, 08:13
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
sina



Joined: 18 Aug 2003
Posts: 132
Location: istanbul turkey
sina
i am sorry i didnot have time to say thanx for all your helpfull efforts
thanks so much..
Post 14 Nov 2003, 01:00
View user's profile Send private message ICQ Number Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
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.
Post 14 Nov 2003, 08:10
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
scientica
Retired moderator


Joined: 16 Jun 2003
Posts: 689
Location: Linköping, Sweden
scientica
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
Post 14 Nov 2003, 10:18
View user's profile Send private message Visit poster's website Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2140
Location: Estonia
Madis731
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 Confused
rdtsc is good, but can it be the same thing with GetTickCount?
Post 14 Nov 2003, 21:41
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
scientica
Retired moderator


Joined: 16 Jun 2003
Posts: 689
Location: Linköping, Sweden
scientica
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
Wink Laughing

_________________
... 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
Post 14 Nov 2003, 21:49
View user's profile Send private message Visit poster's website Reply with quote
sina



Joined: 18 Aug 2003
Posts: 132
Location: istanbul turkey
sina
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
Post 13 Dec 2003, 01:44
View user's profile Send private message ICQ Number Reply with quote
aaro



Joined: 21 Jun 2003
Posts: 107
Location: hel.fi
aaro
Change SetDlgItemText to SetDlgItemInt
Post 13 Dec 2003, 16:35
View user's profile Send private message Reply with quote
sina



Joined: 18 Aug 2003
Posts: 132
Location: istanbul turkey
sina
and scientica the code u have posted crashes can u help me here
Post 13 Dec 2003, 21:55
View user's profile Send private message ICQ Number Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3502
Location: Bulgaria
JohnFound
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. Smile If you want good random numbers, better look for random number generators. There are many algorithms and even very simple.
Post 13 Dec 2003, 22:03
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
sina



Joined: 18 Aug 2003
Posts: 132
Location: istanbul turkey
sina
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
Post 13 Dec 2003, 22:58
View user's profile Send private message ICQ Number Reply with quote
roticv



Joined: 19 Jun 2003
Posts: 374
Location: Singapore
roticv
Why not replace GetSystemTick with
Code:
rdstc
xor eax, edx
    
Post 14 Dec 2003, 05:20
View user's profile Send private message Visit poster's website MSN Messenger Reply with quote
sina



Joined: 18 Aug 2003
Posts: 132
Location: istanbul turkey
sina
yes that worked out great
thanks
Post 14 Dec 2003, 13:28
View user's profile Send private message ICQ Number Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.