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?

share|improve this question
feedback

2 Answers

up vote 2 down vote accepted

The RANDOM command doesn't simply generate a random number for you; what it's actually doing is taking whatever value is already in the variable and transforming it in a pseudorandom way. In other words, the variable you give it is the internal state of the random number generator. You must not modify that variable in any other way if you want the generated numbers to be as random as possible.

When you set randnum = randnum // 2, you are forcing it to be either 0 or 1. It would seem that when the RANDOM command sees a value of 1, it generates a new number that is odd, so you once again set randnum to 1.

What you need to do is use a second variable to hold the binary result:

RANDOM randnum
randbit = randnum // 2

(I can't explain the problem with the loop count changing; that would seem to be a separate issue.)

share|improve this answer
I found the problem with loop count changing. Apparently the memory w0 does not exist for the 08M2. I changed it to b1 and it works. However, even though I applied the changes with randbit, I am still not getting 50-50 occurences. Is there better documentation on how RANDOMworks than on the PICAXE website? – xiongtx Sep 18 at 20:14
feedback
symbol randnum = w0  

FOR action = 1 TO 5
    random randnum
    let randv=randnum & 1  ; Chances of odd and even are equal
    IF randv > 0 THEN
        LOW led_red
        PAUSE 500
        HIGH led_red
        PAUSE 500
    ELSE
        SOUND buzzer,(100, 100)
        PAUSE 500
    ENDIF
NEXT action
share|improve this answer
The comment delimter in PBASIC is ; or ' (not //, which is the modulus operator). – Dave Tweed Sep 15 at 14:18
@DaveTweed:- Thanks fixed, I am not experienced with PBASIC, I gave answer after reading documentation. BTW it implies comment syntax are same as x86 assemblers like NASM. – perilbrain Sep 15 at 15:22
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.