#v1.19.1 (2022-06-18) .uf2
from random import randint
from time import ticks_us, ticks_diff
import array
from micropython import const
SEED = const(0)
control=array.array('i',(999999,240))
@micropython.asm_thumb
def rand_asm2(r0):
ldr(r1,[r0,0]) # seed
ldr(r4,[r0,4]) # max random
label(RANDOM_LOOP)
add(r1,13)
mov(r2,r1)
mov(r3,9)
ror(r2,r3)
eor(r1,r2)
asr(r2,r1,24)
add(r2,127)
cmp(r2,r4)
bgt(RANDOM_LOOP)
label(EXIT)
str(r1,[r0,0])
mov(r0,r2)
@micropython.asm_thumb
def rand_asm(r0,r1):
mov(r4,r1)
label(RANDOM) # uses r1-r5, input=r4, output r2
ldr(r1,[r0,SEED]) # seed
mov(r5,33)
mov(r2,r4) # r4 = max random number
label(HIGHBITS) # counts bits required
sub(r5,r5,1)
lsl(r2,r2,1)
bcc(HIGHBITS) # r5 = max bits
label(RANDOM_LOOP)
add(r1,13) # scramble seed
mov(r2,r1) #
mov(r3,9)
ror(r2,r3)
eor(r1,r2) # end scramble
mov(r2,r1) # r2 = random 32 bit number
str(r1,[r0,SEED])
mov(r3,32)
sub(r3,r3,r5)
asr(r2,r3) # scale down to max random
mov(r3,1)
sub(r5,1)
lsl(r3,r5) # shift 17 bits to get offset
add(r2,r2,r3) # add offset for positive
#bmi(RANDOM_LOOP) # sometimes get negative?
cmp(r2,r4) # get another if too high
bgt(RANDOM_LOOP)
mov(r0,r2)
rand = 1
m=0
odd = 0
tot_array = array.array('i', 0 for _ in range(101))
gticks = ticks_us()
for i in range(1000):
#rand = 1
#rand=randint(0,10)
#rand=rand_asm(control,10)
tot_array[rand] += 1
if not rand % 2:
odd += 1
#b=7
#x=((1<<b)+(seed>>(31-b) ))
#print(x)
if rand>m:
m=rand
#print(rand)
#print('Odd numbers',odd)
diff = ticks_diff(ticks_us(),gticks)
print()
print(diff/1000)
print()
#print(tot_array)