Sometimes it is desirable to be able to generate a random number inside a Galil motion controller. This may used as a foundation for simulating possible IO configurations, testing truly random move lengths, making variable wait times, or other purposes. There are two types of possible random number generations, programatic and physical.

Programatic number generators would take a certain value, typically called the seed value, and perform an algorithm to generate a new number. This seed value is preferred to be changing and somewhat random in order to make the numbers more variable. For this reason, we can use the internal servo clock counter as a seed value for the algorithm. Use of the modulus command ( the '%' command in the DMC-40x0, 41x3 and RIO) is used to make sure the random number stays within a range. A simple example of a calculation like this may look like the following.

`#A`

range=1000 ;' maximum value for random number

a= 4.7 ;' random multiplier, any nonzero number

b= 123 ;' random adder, any nonzero number

#RANDOM

num= ((TIME * a) + b) % range

EN

While this will work, the results from these computations are typically not truly random. This is because of multiple factors, including the fact that calling this function at an expected rate causes the seed value to increment at a known rate. However, this variation does give some random-ness for the range specified.

Physical number generation requires a seed value that is truly non-deterministic to be entered into the system and used in the algorithm to help generate a number. An example of this would be measuring variations on an analog input that were not deterministic (ie. measuring white noise on a floating analog input line). Since most of the controllers analog inputs are by default floating, measuring them gives us slight variation each time we measure that cannot be easily predicted. Further, by adding our programmatic seed value back in, we can make a more robust random number generator.

The following is an example of using a random number generator with a physical input to generate random position values to reach.

range=5000 ;' maximum value for random number

#MAIN

JS#RANDOM

PA num ;'move to random position in specified range

BGX

AMX

WT100 ;'wait 100msec for next move

JP#MAIN

EN

'

#RANDOM

' AN[1] is floating/has white noise input.

' range of 0-4999 for random number

num= @INT[(TIME * @AN[1]) % range]

RE

The routine #RANDOM in the code above can be called whenever a new random number needs to be generated. Even if it is called at a specified time interval, the variability of @AN[1] will prevent the random number generation from having a cyclical repeat.