Random NumberS Work In Messagebox But Not Static Controls

Started by devilhorse, October 10, 2010, 04:45:11 PM

Previous topic - Next topic

devilhorse

Can someone give me a hint as to why the following code does not work like I need it to?
The task is to create two random numbers and display them on static controls. Once this works I will take the two numbers and add them.
The problem is when I run the procedure I named "Randomizer" the same number appears on both static controls. If however, I call Messagebox function, then two unique random numbers appear on the labels and in the messagbox. What I need to know is why the code works correctly after a call to the message box function but fails otherwise?
I have included the EXE in a zip file in addition to the code posted here.


;Random Number Module here The Code To Create Random Numbers Resides
.Const

.Data?
ResultingRandomNumber DWord ?
Rand_Seed1      DWord ?

.Data
bufferRandomNumber Byte 128 Dup(?)
szRightBuffer Byte 16 Dup(?)

.Code

:eek
Randomizer Proc
Invoke InitRandNum
Mov Ebx, Eax
Invoke GetRandNum
   Ret
Randomizer EndP

;
; Initialize random numbers
;
; syntax :
; call InitRandNum
;
; Return:
; eax = number of ticks since the beginning
;
InitRandNum PROC
      push ecx
      push edx

      invoke GetTickCount
      test eax,eax
      jnz Label1
      mov eax,123456789
Label1:   Mov Rand_Seed1,Eax

      pop edx
      pop ecx
   ret
InitRandNum EndP
Align 16
;
; generate a random number between 0 and max value - 1
;
; syntax :
; mov ebx,Range
; call GetRandNum
;
; return :
; eax = the random number
;
GetRandNum PROC
      push edx

      mov eax,987654321
      Mul Rand_Seed1
      bswap eax
      xor eax,078787878h
      jnz Label1
      mov eax,123456789
Label1:   Mov Rand_Seed1,Eax
      cmp ebx,1
      je Label2
      mul ebx
      mov eax,edx
Label2:
      Pop Edx

   ret
GetRandNum ENDP


;Window1 Module
.Const

.Data?
DividedLabel DWord ?
hplusLeftlabel DWord ?
hplusRightLabel DWord ?
addend1 DWord ?
addend2 DWord ?


.Data
PlusSymbol Byte 43,0
DividedSymbol Byte 247,0
MinusSymbol Byte 45,0

DivideSymbolBuffer Byte 6 Dup(?)
strAddend1 Byte 16 Dup(?)
strAddend2 Byte 16 Dup(?)
lu_format     DB  '%s', 0
.Code

Window1Procedure Proc Private hWnd:HWND, uMsg:ULONG, wParam:WPARAM, lParam:LPARAM
   Select uMsg,Eax
   Case WM_CREATE
   Mov DividedLabel,FUNC(GetWindowItem,App.Main,IDC_WINDOW1_STATIC14)
   Invoke lstrcpy,Addr DivideSymbolBuffer,Addr DividedSymbol
   Invoke SetWindowText,DividedLabel,Addr DivideSymbolBuffer

Mov hplusLeftlabel,FUNC(GetWindowItem,hWnd,IDC_WINDOW1_PLUSLEFTLABEL)
Mov hplusRightLabel,FUNC(GetWindowItem,hWnd,IDC_WINDOW1_PLUSRIGHTLABEL)



   Case WM_COMMAND
      Select wParam,Eax
         Case IDC_WINDOW1_STARTBUTTON
         Invoke StartTheQuiz
         
         EndSw
   Case WM_CLOSE
      Invoke IsModal, hWnd
      .If Eax
         Invoke EndModal, hWnd, IDCANCEL
         Return TRUE
      .EndIf
   EndSw
   Return FALSE
Window1Procedure EndP
StartTheQuiz Proc Private
;Fill in the additon problem
Mov addend1, FUNC(Randomizer)
Invoke dwtoa, addend1,Addr strAddend1
;Invoke SetWindowText,hplusLeftlabel,Addr strAddend1 <-------------------------------- THIS TEXT &
Invoke MessageBox,NULL,Addr strAddend1,CTXT("Addend1"),MB_OK

Mov addend2, FUNC(Randomizer)
Invoke dwtoa, addend2,Addr strAddend2
;Invoke SetWindowText,hplusRightLabel,Addr strAddend2 <------------------------------ THIS TEXT ARE THE SAME UNLESS I CALL MESSAGEBOX FUNCTION
Invoke MessageBox,NULL,Addr strAddend2,CTXT("Addend2"),MB_OK
   Ret
StartTheQuiz EndP


Window1timeLabel Proc Private hWnd:HWND, uMsg:ULONG, wParam:WPARAM, lParam:LPARAM
   Return FALSE
Window1timeLabel EndP


Ramon Sala

Hi devilhorse,

I made a quick project using the functions you posted and it works fine. Could you post the whole project (or send it to me) so that I can see what is hapenning? Thanks.


Ramon

rsala@easycode.cat
Greetings from Catalonia

Ramon Sala

Hi again,

Well, the problem is that you call Randomize twice practically at the same time. That is, the GetTickCount API function just controls 1 mS periods of time and the computer executes the two calls to Randomizer in the same 1 mS period. When you call the MesageBox function the values are diferent because some time is elapsed while you accept the message and Randomizer is called again. To avoid this problem, insert some delay betwen the two calls to Randomizer, so just recode the StartTheQuiz procedure like this:

StartTheQuiz Proc Private
    ;Fill in the additon problem
    Mov addend1,  FUNC(Randomizer)
    Invoke dwtoa, addend1, Addr strAddend1
    Invoke Sleep, 10 ;Add some delay between calls
    Mov addend2, FUNC(Randomizer)
    Invoke dwtoa, addend2, Addr strAddend2
    Invoke SetWindowText, hplusLeftlabel, Addr strAddend1
    Invoke SetWindowText, hplusRightLabel, Addr strAddend2
    Ret
StartTheQuiz EndP


Regards,

Ramon
Greetings from Catalonia