1

I have a snippet of my code in which I want to highlight 10 random numbers in the console. So far I have been able to do this, the numbers are generated, but every time I get 10 of the same number

mov ecx, 10
myLoop:
push ecx

;--- randomize a number in the range 0-range ---
    call GetTickCount
    push eax
    call nseed
    push range
    call nrandom
    mov drawnNumber, EAX
;;;;    ................
;--- derivation of the calculation result ---
    push    EAX
    push    OFFSET messageNumber
    push    OFFSET buffer
    call    wsprintfA
    add ESP, 12
    mov rinp, EAX
;--- displaying the result ---------
    push    0 
    push    OFFSET rout
    push    rinp
    push    OFFSET buffer
    push    hout
    call    WriteConsoleA

pop ecx
loop myLoop
IInspectable
  • 46,945
  • 8
  • 85
  • 181
np.
  • 109
  • 1
  • 11
  • 1
    You do not want to reinitialize the random number generator in the loop. Move that to before the loop. As it is, the code likely reinitializes with the same tick since the cpu is running much faster than the tick count increments. – Jester Dec 10 '21 at 01:16
  • but when i move `call nseed` outside of the loop i get infinite loop – np. Dec 10 '21 at 01:28
  • 2
    Did you move all 3 lines? Also check calling convention for `nseed` and `nrandom` as they may be cdecl so you might have to remove the arguments you pushed. In any case that's a different problem :) – Jester Dec 10 '21 at 01:30
  • 1
    Depending on your purpose and requirements, you might consider using the `rdrand` instruction. Simple and easy. (Obviously if your goal is to experiment and understand how random number generators work, that wouldn't be a suitable answer.) – prl Dec 10 '21 at 03:05
  • 1
    Also, you could use a register other than ECX for your loop counter, so you don't need push/pop inside the loop. [The `loop` instruction is slow on most CPUs](https://stackoverflow.com/q/35742570/224132). – Peter Cordes Dec 10 '21 at 03:08

0 Answers0