Using a random number generator in MIPS?

后端 未结 1 410
别那么骄傲
别那么骄傲 2021-01-26 02:46

So I was reading a few threads on this site and I found one on how to make one.. But I can\'t really find a link that explains more about how to code it.. My textbook for the co

相关标签:
1条回答
  • 2021-01-26 02:55

    As stated by MARS documentation (in the GUI Help > Syscalls):

    random int 41 $a0 = i.d. of pseudorandom number generator (any int). $a0 contains the next pseudorandom, uniformly distributed int value from this random number generator's sequence

    So

    li $v0, 41         ; Service 41, random int
    li $a0, 0          ; Select random generator 0
    syscall            ; Generate random int (returns in $a0)
    
    li $v0, 1  ; Service 1, print int
    syscall    ; Print previously generated random int
    

    Works fine for me, every time a different number is printed.

    You don't need to initialize or seed a random stream before using it, you can just use stream 0.

    0 讨论(0)
提交回复
热议问题