Random Command

M.Hancock

New Member
Is there a problem with the Random command in Logigator v3? It seems to work fine on screen but when it's on the PIC the first 3 times the program runs it picks (the same) number bigger than 254 as the random number (which I know it can't - if it is a byte variable) then on the fourth and subsiquent runs it seems to work as expected but still with the occasional 'big number blip'
 

Technical

Technical Support
Staff member
Welcome to the forum, this depends on your viewpoint!

It is working correctly, but probably not as you expected. Because the real PICAXE chip does not have a true 'random' seed (internal clocks are normally used on computers as the seed) the random on the PICAXE chip is basically a mathematical calculation that follows a pattern.

You should not be able to test and see the first, second, third, results etc because we always recommend that random is called multiple times during, for instance, a loop waiting for a switch push. As random is repeatedly called a random number of times, this then gives a better random number.

If you want to post your flowchart we can show you how to do this.
 

M.Hancock

New Member
Thanks for that. I've put the Random command in a switch loop as you suggested and now it's working as I expected.
 

bobmueller

New Member
Basically the same question...

I've got a working circuit that runs the code pasted below. It runs a motor in a random direction, w/ random motor on duration for a random number of loops. It's a cat toy that moves a captured ball via an arm connected to the motor.

I'm using logicator, flowsheet attached. When I run the simulation in the programing tool I get good behavior out of the random. When I down load the program to the chip the random behavior isn't so random anymore... chip seems to favor the forward direction on the motors.

The code below is used to give a 50/50 shot at either going forward or reverse:

Code:
 label_16: random w6 'store random number in w6 (b13:b12)
let varB = b13 'Random command
if varB > 128 then label_17 'Compare command
gosub prc_REVERSE 'Do Procedure
The simulation does this perfectly... the chip always goes forward 90% of the time.

It's not just happening in that loop... when Forward is called the chip will repeat the forward loop 3-5x and when Reverse is called maybe 1 or twice and both loops are set up exactly the same other than the motor command sets pin 1 high.

So how do I get the chip to behave more random?

Also, I'm using compare instead of repeat/loop. Using repeat with a repeat until causes an error at download (duplicate lable name used?) that isn't detected by syntax check.


Code:
symbol varA = b0
symbol varB = b1
symbol varC = b2
symbol varD = b3
symbol varE = b4
symbol varF = b5
symbol varG = b6
symbol varH = b7


let dirs = %00010111


main:
label_1:
label_2:
if pin3 = 1 then label_14 'Decision command
goto label_2

label_14: random w6 'store random number in w6 (b13:b12)
let varA = b13 'Random command
if varA < 245 then label_16 'Compare command
goto label_1

label_16: random w6 'store random number in w6 (b13:b12)
let varB = b13 'Random command
if varB > 128 then label_17 'Compare command
gosub prc_REVERSE 'Do Procedure 
label_42:
low 0
low 1
let varC = 0 'Expression command
let varD = 0 'Expression command
goto label_14

prc_REVERSE:
label_37:
if varD < 215 then label_43 'Compare command
return 'Return 

label_43: random w6 'store random number in w6 (b13:b12)
let varD = b13 'Random command
low 0
high 1
pause 500 'Wait command
goto label_37

label_17: gosub prc_FORWARD 'Do Procedure 
goto label_42

prc_FORWARD:
label_27:
if varC < 215 then label_28 'Compare command
return 'Return 

label_28: random w6 'store random number in w6 (b13:b12)
let varC = b13 'Random command
low 1
high 0
pause 500 'Wait command
goto label_27
 

Attachments

Technical

Technical Support
Staff member
random works best when called repeatedly in a loop. Please see the random command in part 2 of the PICAXE manual (accessed via the Help menu of Logicator).

So ideally you want your randoms as in the image attached.

We cannot get repeat until <-> loop to error, please can you send us an example flowchart of your problem.
 

Attachments

Technical

Technical Support
Staff member
Patch 3.4.1 is now available to download from the software pages to fix this double label error.
 
Top