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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
In 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