Swordfish Module - RandGen


save_f21 Download the module: RandGen

This module implements a simple modulo-based PSEUDO Random Number Generator for Swordfish Basic. It's difficult to generate true random numbers from a deterministic machine like PIC. Hopefully, some mathematical methods let us have a series of numbers that seems really random, although they have all a fixed cycle upon which they restart to give the same series of values. this is why they are all called Pseudo random number generators. The cycling behaviour has the advantage to give you the possibility to repeat the same experience despite the fact that it's based on (pseudo) random numbers.


Usage example

Call the RandGen.Initialize(pInitialValue) one time with the initial value (value read from an analogue floating PIC pin for example).

Call the RandGen.Rand() function to get a new random value.

The following program gives an example on how to use the pseudo-random number generator;

Device = 18F4620
Clock = 20
Include "usart.bas"
Include "convert.bas"
Include "RandGen.bas"
Dim RandomNumber As Byte
// initialize the generator Initial Seed Value to 2
// for Full Random value you can use an ADC read on
// a floating AN PIC Pin
TRISB = 0 'All output
While true
// generates a number
RandomNumber = RandGen.Rand() 
// write to RS233
// visualise number on PORTB leds (on my EasyPic Board)
LATB = RandomNumber

Written by Ahmed Lazreg (octal). A simple Pseudo Random Number Generator module (modulo based).

Link: What is a Swordfish Library?

Forum Activity

  • No posts to display.

Member Access