The random number generator
I wish to generate a random number between 1 and 5 to light a random combination of LED's on the RB port. Only bits RB.0 to RB.4 are used as there are only 5 LED's. I have been messing around and I cannot get the thing to generate random numbers from 1 to 5. I checked the manual but the random statement is not well documented. What is the correct way to approach this?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔

Comments
Post your code so folks can see what you're doing.
Thanks,
PeterM
DEVICE SX28, OSC4MHZ, TURBO, STACKX, OPTIONX FREQ 4_000_000 PROGRAM Start Start: ' Set all ports to outputs: tris_a = 0 tris_b = 0 tris_c = 0 'All ports are off at the start: ra = 0 rb = 0 rc = 0 rnd var byte ' Main loop: DO rnd = RANDOM 5 rb = rnd pause 3000 LOOP▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The // operator returns the remainder of a division; the remainder will always be 0 to the divisor minus one. Don't do the change the original seed as this will limit the "randomness" of your process.
[noparse][[/noparse]Edit] If you're just wanting to active a single output on RB, add this line to my code above:
Post Edited (JonnyMac) : 3/10/2009 4:30:06 AM GMT
rnd var byte fxn var byte rnd = 0 ' Main loop: DO RANDOM rnd fxn = rnd // 3 IF fxn = 0 THEN LightLtoR ELSEIF FXN = 1 THEN LightRtoL ELSE Chase ENDIF LOOP▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Open the IDE. Click on Help->SX/B Help. Click on the "+" next to "Reference" to open it. Click on the "+" next to "Commands" to open it. Scroll as need to the entry labeled "RANDOM" and read it. Your answer is in the section labeled "Explanation".
Thanks,
PeterM
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Thanks,
Lightfoot
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
DEVICE SX28, OSC4MHZ, TURBO, STACKX, OPTIONX FREQ 4_000_000 PROGRAM Start '********************************** Subroutines: ********************************* LightLtoR SUB 0 LightRtoL SUB 0 Chase SUB 0 ChasetoCenter SUB 0 ChasefromCenter SUB 0 Disco SUB 0 '************************************* Main: ************************************* Start: '**************************** Set up output registers: *************************** tris_b = 0 '***************************** Pullup unused pins: ****************************** plp_a = 0 plp_c = 0 '************************ All RB pins are off at the start: ********************** rb = 0 '*********************************** Variables: ********************************** i2 var byte i var byte 'For loop index variable. exp var byte 'Working storage variable for math computations. '*********************************** Main loop: ********************************** DO LOOP '************************ Letters chase from right to left: ********************** SUB LightRtoL exp = 32 FOR i = 1 TO 5 exp = exp / 2 RB = RB + exp pause 1000 NEXT RB = 0 pause 1000 ENDSUB '************************ Letters chase from left to right: ********************** SUB LightLtoR exp = 1 FOR i = 1 TO 5 exp = exp * 2 RB = exp - 1 pause 1000 NEXT RB = 0 pause 1000 ENDSUB '************************** Letters chase back and forth: *********************** SUB Chase exp = 1 FOR i = 1 TO 5 exp = exp * 2 RB = exp - 1 pause 150 NEXT RB = 0 exp = 32 FOR i = 1 TO 5 exp = exp / 2 RB = RB + exp pause 150 NEXT RB = 0 ENDSUB '************************** Letters chase towards center: *********************** SUB ChasetoCenter exp = 8 RB = 25 FOR i = 1 TO 3 rb = rb - exp exp = exp - 1 pause 150 NEXT RB = 0 ENDSUB '************************** Letters chase from the center: *********************** SUB ChasefromCenter exp = 6 RB = 4 pause 150 FOR i = 1 TO 2 rb = rb + exp exp = exp + 1 pause 150 NEXT RB = 0 ENDSUB '************************ Letters flash like a strobe light: *********************** SUB Disco rb = 0 pause 30 rb = 255 pause 30 ENDSUBIn case you are curious, the pins RB.0 to RB.4 are connected to a FET buffer. These power an array of LEDs arranged in a pattern of 5 letters. It is a sign I am making. It will read "ECTET"
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Does it truly matter if you have "actual" random values versus having "pseudo-random"? All random number generators in all languages use pseudo-random generators. The idea is that they appear random, and no human is likely to memorize the complete pattern. If you have an byte sized random value, then there are 256 different outputs until the pattern repeats. I have attached a tiny program that is a stripped down version of the Digital Dice program. Here are the outputs it generated:
The first number in each pair is the value that came out of the random function, and the second number is value scaled from 0 to 5. These correspond to the variables called "CurrRandom" and "ZeroToFive". Every time you run this program, you'll get the same sequence, but the fact is that while you may be able to memorize the first few outputs, you'll lose track long before you hit number 257 where the cycle repeats.
If you run this in the debugger, you'll can see the values in the watch window. Just set the breakpoint on the line that says:
BTW, I used an SX48 for testing. If you use the SX28, just comment out the SX48 "Device" line and un-comment the SX28 line.
Thanks,
PeterM