Author Topic: Random seed for random algorithm example  (Read 517 times)

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 5764
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Random seed for random algorithm example
« on: April 13, 2018, 12:57:32 AM »
Simple and fast enough. The algo does not need the 1000 iterations, 5 or 10 would do but its fast enough to run a high count.

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    include \masm32\include64\masm64rt.inc

    .code

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

entry_point proc

    LOCAL lcnt  :QWORD

    mov lcnt, 20

  lbl:
    call reseed
    conout str$(rax),lf
    sub lcnt, 1
    jnz lbl

    waitkey
    .exit

entry_point endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

 NOSTACKFRAME

 reseed proc
  ; --------------------------------------
  ; random algorithm random seed generator
  ; --------------------------------------
    LOCAL var1  :QWORD

    lea r10, var1                           ; load the address
    mov r9, 1000                            ; set the counter
    mov r11, 12345678                       ; put something in r11

  @@:
    invoke QueryPerformanceCounter, r10     ; call the API
    mov rax, [r10]                          ; write value in var1 to rax
    bswap rax                               ; byte swap rax
    xor r11, rax                            ; xor combine with r11
    sub r9, 1                               ; decrement counter
    jnz @B                                  ; loop again if not 0

    mov rax, r11                            ; return xorred content in rax

    ret

 reseed endp

 STACKFRAME

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    end
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

wreckItRalph

  • Regular Member
  • *
  • Posts: 6
Re: Random seed for random algorithm example
« Reply #1 on: April 27, 2018, 03:42:48 AM »
Thats SO much shorter than Mersenne-Twister implementations in assembly Ive seen!

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 5764
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Random seed for random algorithm example
« Reply #2 on: April 27, 2018, 08:36:24 AM »
Ralph,

Note that this algo is for seeding a random number generator, it is not a random number generator in itself.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin: