I'm wondering how to effectively generate random numbers with my PICAXE 08M2. The following is my code:
; Inputs
symbol push_red = C.1 ; In 1
symbol push_buzzer = C.3; In 3
; Outputs
symbol buzzer = C.2; Out 2
symbol led_red = C.4;
symbol action = b0
symbol randnum = w5;
symbol randbit = w6;
main:
FOR action = 1 TO 5
RANDOM randnum
LET randbit = randnum // 10
IF randnum > 4 THEN
PAUSE 500
low led_red
pause 500
high led_red
ELSE
SOUND buzzer,(100, 100)
ENDIF
NEXT action
SOUND buzzer,(120, 200)
Even though I'm generating a new random number with every loop, the result is always the same: LEDs light up, no sound generated. I've read some articles about dividing randnum
by an appropriate number, but not sure how that works.
Best Answer
Like Dave says: you're using the wrong variable in the
IF
statement.should be
As I understand from another question
randnum
is in the range 0..65535, then the probability that it's less than 5 is less than 1 in 10 000. So if you repeat the test a lot of times you'll hear the buzzer once or twice, but those chances are slim for a loop executed only 5 times.