My random number generator is not random or is it my programming?
I will be using an 18x to light eight LEDs with any one of them switching off for a short period. The one to be switched of is selected by a random number generator.
I am using the experimenter board and initially just the three supplied LEDs, green, yellow and red. I divided the random number byte (0-255) by three and selected which LED was off by a simple numerical filter.
Because a truly random number would allow the same LED to be selected repeatedly, I added some code so that if the same LED were to be selected again a new random number would be calculated.
Unfortunately, this approach does not work as I have never seen a green to red or a red to green transition and I counted, over a minute or so, 13 green, 26 yellow and 14 red lights.
What am I doing wrong?
To complicate matters further, if I run the code in the simulator I get different results, with all possible transitions appearing and the number of each of the three LEDs being selected being very similar to each other. That is, they appear truly random.
symbol R_LED = 0
symbol Y_LED = 1
symbol G_LED = 2
symbol randNum =b2
symbol previous = b6
symbol current = b7
main:
rand_LED:
random w0 'generate random number
randNum = b0
let previous = current
let pins = %00000000
if randNum >= 170 then two 'filter which LED to switch off
if randNum >= 85 and randNum <= 169 then goto one
if randNum <= 84 then zero
two:
let current = 2
if current = previous then goto rand_LED
high R_LED
pause 1500
goto main
one:
let current = 1
if current = previous then goto rand_LED
high Y_LED
pause 1500
goto main
zero:
let current = 0
if current = previous then goto rand_LED
high G_LED
pause 1500
goto main
PS. To make this easier to see, the LEDs are inverted. That is, all are normally off and the randomly selected one on.
I will be using an 18x to light eight LEDs with any one of them switching off for a short period. The one to be switched of is selected by a random number generator.
I am using the experimenter board and initially just the three supplied LEDs, green, yellow and red. I divided the random number byte (0-255) by three and selected which LED was off by a simple numerical filter.
Because a truly random number would allow the same LED to be selected repeatedly, I added some code so that if the same LED were to be selected again a new random number would be calculated.
Unfortunately, this approach does not work as I have never seen a green to red or a red to green transition and I counted, over a minute or so, 13 green, 26 yellow and 14 red lights.
What am I doing wrong?
To complicate matters further, if I run the code in the simulator I get different results, with all possible transitions appearing and the number of each of the three LEDs being selected being very similar to each other. That is, they appear truly random.
symbol R_LED = 0
symbol Y_LED = 1
symbol G_LED = 2
symbol randNum =b2
symbol previous = b6
symbol current = b7
main:
rand_LED:
random w0 'generate random number
randNum = b0
let previous = current
let pins = %00000000
if randNum >= 170 then two 'filter which LED to switch off
if randNum >= 85 and randNum <= 169 then goto one
if randNum <= 84 then zero
two:
let current = 2
if current = previous then goto rand_LED
high R_LED
pause 1500
goto main
one:
let current = 1
if current = previous then goto rand_LED
high Y_LED
pause 1500
goto main
zero:
let current = 0
if current = previous then goto rand_LED
high G_LED
pause 1500
goto main
PS. To make this easier to see, the LEDs are inverted. That is, all are normally off and the randomly selected one on.