HELP: Generating random numbers in PASM
Bean
Posts: 8,129
I got this code from Chip's interpreter.spin
The "z" flag is set or cleared for a forward or backward random calculation. I only need to do the forward (z set). So I have optimized the code as follows:
But it doesn't seem to be working right. If I plot the random values, I get a diamond pattern not a random pattern.
I can't figure out what I am doing wrong.
Bean
:rnd min x,#1 '?var/var? mov y,#32 mov a,#%10111 if_nz ror a,#1 :rndlp test x,a wc if_z rcr x,#1 if_nz rcl x,#1 djnz y,#:rndlp wc 'c=0 jmp #:stack
The "z" flag is set or cleared for a forward or backward random calculation. I only need to do the forward (z set). So I have optimized the code as follows:
min x,#1 ' RANDOM value mov y,#32 __L0001 test x,#%10111 WC rcr x,#1 djnz y,#__L0001 WC
But it doesn't seem to be working right. If I plot the random values, I get a diamond pattern not a random pattern.
I can't figure out what I am doing wrong.
Bean
Comments
If I want a random number between 0 and x do I just return the "value MOD x" ?
Bean
The random values are not random. If you run a sequence like You'll get cyclical numbers. You need a real analog input to get a real random number. That's why we have the real random object in the OBEX.