Random ARMv7汇编,如何改进这个伪随机数生成器?
我有一个伪随机函数,它是以unix epotch作为种子的时间为基础的,我需要得到4个1-5之间的数字,目前我要做的是每次得到一个数字并打印出来。这最初是针对0-9之间的数字,并将其更改为1-5。我意识到数字6-9将始终导致5,这意味着5比任何其他数字发生的总概率更高 这只是一个简单的猜谜游戏,所以它没有做任何事情Random ARMv7汇编,如何改进这个伪随机数生成器?,random,raspberry-pi,arm,armv7,Random,Raspberry Pi,Arm,Armv7,我有一个伪随机函数,它是以unix epotch作为种子的时间为基础的,我需要得到4个1-5之间的数字,目前我要做的是每次得到一个数字并打印出来。这最初是针对0-9之间的数字,并将其更改为1-5。我意识到数字6-9将始终导致5,这意味着5比任何其他数字发生的总概率更高 这只是一个简单的猜谜游戏,所以它没有做任何事情 timeofday: MOV r7, #78 @gettimeofday returns time LDR r0, =time @add
timeofday:
MOV r7, #78 @gettimeofday returns time
LDR r0, =time @address of holder for time_t
MOV r1, #0 @timezone not needed
SWI #0
LDR r0, =time @set r0 back to label addr
LDRB r1, [r0, #4] @load epotch value from r0 into r1
MOV PC, LR @back to calling location, Then it will branch to the function below.
fakemod:
CMP r1, #10 @Ensuring we don't create a negative
SUBGE r1, r1, #10 @decrease r1 by 10 until it's a single digit
CMP r1, #10
BGE fakemod @if the number in r1 is still greater than 10 loop
singledigit: @Need a value between 1-5
CMP r1, #5 @Checks in r1 has a value larger than 5
SUBGT r1, r1, #1 @subtracts if r1 is a integer greater than 5
CMP r1, #5 @compares if the number is greater than 5 still
BGT singledigit @if greater than 5 go back to loop start.
CMP r1, #0 @needs to be between 1-5, so if it's 0, just add 1
ADDEQ r1, r1, #1
MOV PC, LR @once it's an integer between 1-5, go back to call location.
我没有连续地从大于5的值中减去1,然后将1加上0,而是尝试了以下方法,结果是“垃圾”:
timeofday and fakemod loop are the same.
CMP r1, #5
BGT timeofday
CMP r1, #0
BEQ timeofday
我将fakemod函数改为如下,得到了理想的结果。在随机数生成器中,将1添加到0并不理想,但它足以满足我的需要。这是几周前写的,所以我有点忘记了目的
fakemod:
CMP r1, #5 @Ensuring we don't create a negative
SUBGT r1, r1, #5 @decrease r1 by 5 until it's a single digit
CMP r1, #5
BGT fakemod @if the number in r1 is still greater than 10 loop
CMP r1, #0
ADDEQ r1, r1, #1
MOV PC, LR @once it's an integer less than 5, go to top.