Always use a word variable with the RANDOM command. If a byte variable is used the sequence of different numbers returned by RANDOM is very limited. Be careful not to use the variable used in the RANDOM command elsewhere in your code; writing to it will disturb the random sequence.

To calculate a random number between zero and N-1 use ...

-- RANDOM w0

-- w1 = w0 //

To calculate a random number between 1 and N use ...

-- RANDOM w0

-- w1 = w0 //

So to determine a random dice roll of between 1 and 6 ...

-- RANDOM w0

-- w1 = w0 // 6 + 1

The // operator is 'modulus'. It returns the remainder after a division by the following number. So if 'w0' happened to be 9, 9 // 6 = 3, add 1 and we would have rolled a 4, which will be placed in the 'w1' variable.

The result variable ( 'w1' in examples above ) does not have to be a word variable, but can also be a byte variable if the number is less than 256 ...

-- RANDOM w0

-- b2 = w0 // 6 + 1

Note we cannot use 'b0' or 'b1' because they are part of 'w0' and this would alter 'w0' and mess up the random sequence.

Sometimes the random numbers are just not random enough. There are two solutions, firstly to divide down the random number before applying the modulus, and secondly to check that the random number generated is not the same as the last one. Both mechanisms can be used together ...

-- DO

---- RANDOM w0

---- b2 = w0 / 199 // 6 + 1

-- LOOP UNTIL b2 <> b3

-- b3 = b2

It can take a bit of experimenting to find the best value to divide by ( 199 in the above example - chosen for no particular reason ), and it is a good idea to write a short program which displays the numbers generated using SEROUT or SERTXD so you can check the numbers look okay for use, and the divisor can be adjusted appropriately.

To calculate a random number between zero and N-1 use ...

-- RANDOM w0

-- w1 = w0 //

*N*To calculate a random number between 1 and N use ...

-- RANDOM w0

-- w1 = w0 //

*N*+ 1So to determine a random dice roll of between 1 and 6 ...

-- RANDOM w0

-- w1 = w0 // 6 + 1

The // operator is 'modulus'. It returns the remainder after a division by the following number. So if 'w0' happened to be 9, 9 // 6 = 3, add 1 and we would have rolled a 4, which will be placed in the 'w1' variable.

The result variable ( 'w1' in examples above ) does not have to be a word variable, but can also be a byte variable if the number is less than 256 ...

-- RANDOM w0

-- b2 = w0 // 6 + 1

Note we cannot use 'b0' or 'b1' because they are part of 'w0' and this would alter 'w0' and mess up the random sequence.

Sometimes the random numbers are just not random enough. There are two solutions, firstly to divide down the random number before applying the modulus, and secondly to check that the random number generated is not the same as the last one. Both mechanisms can be used together ...

-- DO

---- RANDOM w0

---- b2 = w0 / 199 // 6 + 1

-- LOOP UNTIL b2 <> b3

-- b3 = b2

It can take a bit of experimenting to find the best value to divide by ( 199 in the above example - chosen for no particular reason ), and it is a good idea to write a short program which displays the numbers generated using SEROUT or SERTXD so you can check the numbers look okay for use, and the divisor can be adjusted appropriately.

Last edited: