I'm trying to use the random
command in PBASIC and I'm not sure how it works. I'm using a PICAXE-08 Proto Board Kit, and the chip specifically is a PICAXE-08M2.
Here is the code I'm using:
symbol randnum = w0
RANDOM randnum
randnum = randnum // 2
FOR action = 1 TO 5
IF randnum > 0 THEN
LOW led_red
PAUSE 500
HIGH led_red
PAUSE 500
ELSE
SOUND buzzer,(100, 100)
PAUSE 500
ENDIF
NEXT action
(Perhaps due to assembly mistakes, the LED's HIGH corresponds to LOW and vice-versa).
First of all, I expected to put the RANDOM randnum
command within the for loop, but if I do this, the loop count becomes incorrect. Instead of 5 actions, I get only 2.
The second problem is that I'm expecting randnum
to be either 0 or 1, so that 50% of the time the LED is lit and 50% of the time the buzzer sounds. However, the LED is lighting 100% of the time, even across multiple trials. Obviously, randnum
is never 0. However, according to documentation, RANDOM
should give the variable a value between 0 and 65535, inclusive.
What is the issue here?