RANDOM Discrepancy?

Odessa

Senior Member
In Manual 2, under the 'Random' command, is stated:

"On computers a changing quantity (such as the date/time) is used as the start of the calculation, so that each random command is different. The PICAXE does not contain such date functionality, and so the sequence it generates will always be identical unless the value of the word variable is set to a different value before the random command is used."

In using the simulator (08M), I notice that if the same seed is repeatedly used, DIFFERENT random numbers are generated, not the same 'random' number as the text indicates would be the case in an operating 08M with the same algorithm always operating on that same seed.

Try this (leave the seed at 0 in the analog input field in the simulator, and you will see that W5 takes on different values, over and over again):

RndCalc:
W5=W6
ReadADC10 1,W6
Random W6
Pause 1000
If W6<>W5 then Goto RndCalc

I watched for perhaps 10 iterations and did not notice any repeat sequences, nor did W6 ever equal W5 (although, eventually this is bound to occur).

Is the simulator working differently than the hardware does, or has the PicBasic 'Random' command been updated to operate as the simulator does? Perhaps the command might now use an internal timer running asynchronously to the command, thus producing a modified algorithm on each iteration even though the seed remains the same?

Thanks, Odessa.

PS I know I could run tests to determine this, however, before doing so, if necessary, I am trying to find out if anyone may have already done that, or otherwise determined the answer to this issue.

Thanks, Odessa
 

hippy

Ex-Staff (retired)
The PICAXE behaves as described in the manual; takes the previous value of the variable and determines a new value solely from its current value.

The Simulator uses a different algorithm. I believe this is by design, so anyone wishing to test programs using random numbers does not have to repeatedly loop through 'seeding' code to generate unique random numbers.
 

Odessa

Senior Member
Ok, Hippy, thanks for your quick reply.

If the simulator has been programmed to intentionally NOT simulate the hardware, I recommend adding a note about this, and why (as you explained in your post), under the 'Random' command in Manual 2.

Thanks, Odessa.
 
Top