Positron Compiler Documentation

Seed

Positron8 · Revision 4.0.6.5 · PDF page 352

Seed Value

Seed the pseudo random number generator, in order to obtain a more random result.

Value can be a variable, constant or expression, with a value from 1 to 65535. A value of $0345 is a good starting point.

' Create and display a Random number
  Device = 18F26K40          ' Compiler for an 18F device
  Declare Xtal = 20          ' Tell the compiler the device is operating at 20MHz
  Declare Hserial_Baud = 9600  ' Choose the Baud rate for HRsoutLn

  Dim wMyRandom as Word

  Seed $0345               ' Seed the pseudo random generator with the value $0345

  Do                         ' Create a loop
     wMyRandom = Random      ' Generate a pseudo random value into wMyRandom
     HRsoutLn Dec wMyRandom  ' Display the pseudo random value on an serial terminal
     DelayMs 500             ' Create a delay so the values can be seen
  Loop                      ' Do it forever

All microcontrollers use a pseudo random number generator because they do not have a random noise generator peripheral, so the parameter that is used for Seed will always produce the same sequence of random values from the Random command.

It is important to be aware of this because each microcontroller will produce the same pseudo random values. If true random number generation is required, find a source of random noise that can be fed into the microcontroller’s ADC and use this value to place in the Seed command.

See also: Random.

View original PDF page 352Open page in PDFOriginal PDF page 352