问题描述
我正在对ARM中的数字游戏进行猜测,以了解更多信息.但是我对随机数的产生感到困惑...我调用rand(),但是我不知道如何做模100来生成0-100范围内的数字.我认为ARM中没有模数,并且和r0,r0,#100"不是很随意...
I'm coding a guess the number game in ARM to learn more about it.But i'm stuck at the random number generation...I call rand() but I don't know how to do the modulo 100 to generate a number in the range 0-100.I don't think there is modulo in ARM and "and r0, r0, #100" is not very random...
这是伪随机数生成的开始:
Here is the start of the pseudo random number generation :
mov r0, #0
bl time
bl srand
bl rand
推荐答案
仅当您感兴趣的其余部分来自除法的幂为2时,AND才起作用.您可以做的一件事是改用一个这样的值的100.
ANDs only work if the remainder you are interested in comes from the division with a power of 2. One thing you could do would be to use one such value instead of 100.
一种替代方法是只使用余数定理:
An alternative would be just to use the remainder theorem:
a%100 = a-(100 * int(a/100))
这也是gcc的功能(尽管它实际上避免了用倒数;但是,如果您的ARM支持整数除法指令,则您幸运).
This is what gcc does as well (though it avoids actually dividing with 100 by something called reciprocal multiplication; if your ARM supports integer divide instructions, though, you're lucky).
这篇关于带模ARM的随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!