Random number in PASM
Bean
Posts: 8,129
In the PropBASIC compiler I'm using the same code to generate a random number as spin uses. But it's not working right, it seems to be random for a little while (maybe 50 or so calls) then it gets "stuck" and generates the same number everytime.
I must admit I'm not sure what the code is doing, but I though I copied it's function exactly but I can't seem to see where I went wrong.
The top code is generated by the PropBASIC compiler, the bottom code is from the interpreter.spin file that chip posted.
I'm not sure why the "if_nz" condition is on the "ror a,#1" line ??? It's not in even in the loop...
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Does that byte of memory hold "A", 65, $41 or %01000001 ?
Yes it does...
·
I must admit I'm not sure what the code is doing, but I though I copied it's function exactly but I can't seem to see where I went wrong.
min seed,#1 ' RANDOM seed, x mov __temp1,#32 mov __temp2,#%10111 IF_NZ ror __temp2,#1 __L0002 test seed,__temp2 WC IF_Z rcr seed,#1 IF_NZ rcl seed,#1 djnz __temp1,#__L0002 WC mov X,seed :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 top code is generated by the PropBASIC compiler, the bottom code is from the interpreter.spin file that chip posted.
I'm not sure why the "if_nz" condition is on the "ror a,#1" line ??? It's not in even in the loop...
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Does that byte of memory hold "A", 65, $41 or %01000001 ?
Yes it does...
·
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
24 bit LCD Breakout Board coming soon. $21.99 has backlight driver and touch sensitive decoder.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chip Gracey
Parallax, Inc.
I changed it to this, and it seems to be working now. Thanks.
Bean
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Does that byte of memory hold "A", 65, $41 or %01000001 ?
Yes it does...
·
Here is a more efficient version:
If you don't need all 32 bits changed, you can do this:
To break up·the LFSR·pattern, you could periodically do 'add seed,#1'.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chip Gracey
Parallax, Inc.
While I have you, I have a problem with the Propeller Tool IDE.
When I compile a file, the PropBASIC IDE calls the propeller tool with the newly created filename. Problem is, if the propeller tool already has the old version of that file open, it doesn't reload it. So you must manually close the file everytime in the propeller tool IDE. Is there any chance of having the propeller tool reload the new version of the file ?
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Does that byte of memory hold "A", 65, $41 or %01000001 ?
Yes it does...
·
1. One LFSR transition:
2. One LFSR transition, and add one to the seed each time:
3. 32 LFSR transitions:
-Phil
Algorithm:
1. Add a value
2. Rotate the bits
After many attempts I found out that addValue = $8080_8080 and rotateValue = #17 produces a very good result. (white noice)
By ear I found out that addValue = $9030_3090 and rotateValue = 16 is very close to the the noise generation of the real SID. (even though a real SID uses a MUCH more complex noise generation)
Lawson
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Lunch cures all problems! have you had lunch?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
24 bit LCD Breakout Board coming soon. $21.99 has backlight driver and touch sensitive decoder.
I've been pondering random numbers for some time. I want a network of boards to talk wirelessly after a random time. The problem is they all start off with the same seed and then all generate the same delay and all talk at once. It is not easy, as where do you get a truly random seed? From the time/date on a clock chip? From the temperature? From the delay to the first keypress (but that doesn't work if the board is autobooting and there is no user input). Is it worth devoting a pin on the propeller to reading the quantum noise of a transistor junction?
Some nifty code examples above that I might borrow, if I may.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
www.smarthome.viviti.com/propeller
Or use my I'd object for a seed.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
24 bit LCD Breakout Board coming soon. $21.99 has backlight driver and touch sensitive decoder.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
www.smarthome.viviti.com/propeller
At the beginning of RealRandom.spin in the OBEX is·a hard-won description of real random phenomena and why it requires something·analog and chaotic·from beyond the digital domain.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chip Gracey
Parallax, Inc.
It's quite obvious that my algorithm doesn't produce white noise and you can easily see patterns in the graph.
That doesn't matter in my case, because I don't want to have white noise anyway.
What did you use to plot the graph, i want to play around with it myself.
It's not very hard to code my own graph application, but if there's an alternative i'm interested.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
24 bit LCD Breakout Board coming soon. $21.99 has backlight driver and touch sensitive decoder.
Jonathan
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
lonesock
Piranha are people too.
Chip,
Yup, I misunderstood the intent of your "add 1" comment
-Phil
I can't see anything wrong in the code above.
I will do my own little graph with c++ + SDL soon.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
24 bit LCD Breakout Board coming soon. $21.99 has backlight driver and touch sensitive decoder.
X = Itteration
Y = 32 bit random number
Jonathan
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
lonesock
Piranha are people too.
I've found that either replacing or adding an ADD-with-carry makes more Noise and less patterns.
And seeds can't get more random than adding how many microseconds it takes someone to press a button,
which seems to fit with the absolutely deterministic nature of Logic, and also the popular
theory of the randomizing effects of an "observer" on quantum mechanical phenomena.
Seriously, some LFSRs sound like MUSIC, and simply changing an XOR to ADDC gets WHITE NOISE instead.
My first apparently somewhat successful attempted pseudorandom number generator went something like
A=A+1
B=B XOR A
D=D ADC B
but it was used to get a Star Trek warp drive effect, and tended to occasionally appear to send stars
flying out from the center in the same random direction just a wee bit too often (such as 2 to 4 in a row).
Then again, I think I've proved that out of all the numbers that come up in a game of chance such as
Roulette, the last number immediately repeats more often than anybody's favorite number comes up.
Doesn't the Spin "?" function have to cycle through all other numbers before repeating one?
It certainly tends to make comets instead of stars when you plot points from it.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
24 bit LCD Breakout Board coming soon. $21.99 has backlight driver and touch sensitive decoder.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
24 bit LCD Breakout Board coming soon. $21.99 has backlight driver and touch sensitive decoder.
The way to test an n-bit-long LFSR for maximal run length is to seed it with a non-0 value (like 1), then iterate it up to 2^n times with a fixed tap set. If the initial number reappears, before 2^n - 1 iterations, you have a dud. If you never get the initial number back, you have a dud. Duds get caught up in sub-maximal loops, which may or may not include the initial number. If you get the initial number back on the 2^n - 1th iteration, you have a maximal-length tap set, which is ideal. To get the initial number back at the 2^n - 1th iteration, the LFSR must have cycled through every non-0 possibility, which gives you the most bang for the number of bits in the LFSR.
Here's a page that lists maximal-length LFSR taps sets by LFSR bit length:
http://www.ece.cmu.edu/~koopman/lfsr/index.html
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chip Gracey
Parallax, Inc.
Post Edited (Chip Gracey (Parallax)) : 12/2/2009 2:20:26 AM GMT
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
My Prop Info&Apps: ·http://www.rayslogic.com/propeller/propeller.htm
The code I used for my XORshift RNG
It worked well enough for some test signal generation code.
Lawson
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Lunch cures all problems! have you had lunch?
Jonathan
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
lonesock
Piranha are people too.