random varible

I'm trying to figure out how to get a variable to generate a random value between 1 and 4 then turn on a bulb that corresponds to that value but so far I'm just getting syntax errors, if you can point out the faults I would be very thankful
Code:
symbol delta = b0
max; b0 = 4
min; b0 = 1
random delta
if delta = 1 then{
        high b.4
        wait 2
        low b.4}
if delta = 2 then{
        high b.5
        wait 2
        low b.5}
if delta = 3 then{
        high b.6
        wait 2
        low b.6}
if delta = 4 then{
        high b.7
        wait 2
        low b.7}
set; b0 = 0
 

Buzby

Senior Member
Random needs a dedicated word variable to use. It must not be reset to zero after each use. The result is always a 16 bit number with the bits pseudo-randomly set.

The line with the modulo divide ( // ) makes a number from 1 to 4.

Note: There are a couple of gotchas with the random function.
First, the random number sequence will always be the same each time the Picaxe is started. There are a couple of solutions to this.
Second, the next random number is reasonably predictable, if you put enough effort in !. There is a solution to this as well.

( It's too late for me to go through the solutions tonight. )



Code:
symbol deltaRnd = w0
symbol delta    = b2

do    
    random deltaRnd  ' Run the 16 bit random number generator

    delta = deltaRnd // 4 + 1 ' Make result 1 to 4 

    if delta = 1 then{
            high b.4
            wait 2
            low b.4}  
    endif

    if delta = 2 then{
            high b.5
            wait 2
            low b.5}
    endif

    if delta = 3 then{
            high b.6
            wait 2
            low b.6}
    endif    

    if delta = 4 then{
            high b.7
            wait 2
            low b.7}
    endif

loop
 

hippy

Ex-Staff (retired)
I have usually used something like below to make the number feel more random -

Code:
Symbol randomNumber = w1 ; b3:b2
Symbol this         = b4
Symbol last         = b5

Do
  Random randomNumber
  this = randomNumber / 17 // 4 + 1
  If this <> last Then
    last = this
    SerTxd(#this, " ")
  End If
Loop
The '/ 17' makes the number less sequential, the '// 4' creates a value 0 to 3. Using '& 3' would do the same in this case and be quicker. The '+ 1' makes it 1 to 4 though you could also handle 0 to 3 and avoid having that. Using 'this' and 'last' prevents the same number being used consecutively because that can make it feel non-random even though it is.
 

Buzby

Senior Member
If the input is just left floating, it will act like an aerial and generate random data. .
I wouldn't trust a floating input to give reliable random data. It's just as likely to stick at '1' or '0', then switch when some external effect happens.

This happened on a breadboard project I was developing. An input was floating because I'd not yet finished the hardware, but the code relating to the input was partly written. The project had some strange behaviour, which I traced to being due to the proximity of my hand near the breadboard.

Now I never leave an input floating during project development, and finished projects use 'PULLUP' to tame any unused pins.
 
Top