问题描述
因此,我在该站点上阅读了一些主题,然后发现了一个关于如何制作主题的主题.但是我真的找不到一个链接来解释更多有关如何编码的链接.我在这门课程的教科书中根本没有提供有关RNG的任何信息,因此在那里没有任何帮助.
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 course didn't provide any information about RNG at all so no help there.
代码是
li $a1, 4
li $v0, 42
add $a0, $a0, 1
这对于要求1-3之间的范围是否正确?我尝试输出它是什么随机数,但它不断为我提供相同的数字.
is this correct for asking for a range between 1-3?I tried outputting what random number it was but it gave me the same number constantly.
#sw $a0, 0($s0)
li $a1, 4
li $v0, 42
add $a0, $a0, 1
#syscall
li $v0, 4
la $a0, Checking
syscall
li $v0, 1
move $t0, $a0
syscall
我看到了sw $a0, 0($s0)
,但是我不确定该怎么做-是否需要输出? (我把它拿出来是因为当我按下一个键进入RNG之后,它说程序崩溃了)我一直得到268501267的输出,我不确定那是什么意思现在它开始一直给我268500992
I saw the sw $a0, 0($s0)
but I'm not sure what that does -- is it needed to output? (I took it out because after I pushed a key to go to the RNG, it said the program crashed)I keep getting the output of 268501267 which I'm not sure what that meansedit: now it started giving me 268500992 all the time
任何人都可以帮忙深入解释一下吗?
Can anyone help explain this a little more in depth?
从逻辑上讲-我理解42的来源以及为什么我需要加+1(这样我就不会得到0的值)
Logically speaking -- I understand the where the 42 comes from and why I need to add +1 (This is so I won't get the value of 0)
从那里开始,我不知道代码为什么不输出我给定范围内的数字.
From there, I have no clue on why the code won't output a number in the range I gave.
推荐答案
如 MARS文档(在GUI帮助> Syscall中):
As stated by MARS documentation (in the GUI Help > Syscalls):
所以
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.
使用流之前,您无需初始化或播种随机流,只需使用流0.
You don't need to initialize or seed a random stream before using it, you can just use stream 0.
这篇关于在MIPS中使用随机数生成器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!