PseudoRandomNumbers
Here is some Pseudo random number generator modules that I use alot and I have ported to Firewing.
Thanks to David Eather and Ahmed Lazreg (Octal) for the original code.
Example of using all modules:
' Imports section... imports RandByte imports RandUshort imports RandGen ' Declarations... dim i as byte = 0 dim x as ushort = 0 dim y as uinteger = 0 Sub Main() RandByte.InitializeRNDbyte(4)'if desired RandUshort.InitializeRNDushort(4)'if desired RandGen.InitializeRandGen(119)'if desired RandGen.SetRndMax(10)'return a random number between 0 - 9 while true ' get random number from byte/ushort... i = GetRNDbyte() x = GetRNDushort() y = GetRndNum() ' write data to console... Console.Write("byte = ",str(i)," ushort = ",str(x)," RandGen = ",str(y),13,10) delayms(1000) end while End Sub
Example of Console output:
Module One - returns Random Byte:
'***************************************************************************** '* Name : RandByte.BAS * '* Author : Gavin Wiggett * '* : * '* Date : 20/05/2013 * '* Version : 1.0 * '* Notes : call InitializeRNDbyte(pValue) with a value between 0-255 * '* : call GetRNDbyte() to get a Byte 0 to 255 * '* : many thanks goes to David Eather for the original code. * '***************************************************************************** Module RandByte Dim LCG as byte = 84 dim GLFSR As Byte = 1 Public Function GetRNDbyte() As Byte 'LCG LCG=(7*LCG+17) 'Galios LFSR If (GLFSR And 1) = 1 Then GLFSR = GLFSR Xor 135 '135 is the tap GLFSR = (GLFSR >> 1) Or &H80 Else GLFSR = (GLFSR >> 1) End If return GLFSR Xor LCG End Function Public Sub InitializeRNDbyte(ByVal ReSeed As Byte) LCG = ReSeed GLFSR = LCG Xor &H55 'just making the start values very different - not realy important If GLFSR = 0 Then 'except that GLFSR must not be zero GLFSR=1 End If End Sub End Module
Module Two - returns Random Ushort:
'***************************************************************************** '* Name : RandUshort.BAS * '* Author : Gavin Wiggett * '* : * '* Date : 20/05/2013 * '* Version : 1.0 * '* Notes : call InitializeRND(pValue) with a value between 0-65535 * '* : call GetRND() to get a Byte 0 to 65535 * '* : many thanks goes to David Eather for the original code. * '***************************************************************************** Module RandUshort Dim LCG as ushort = 21844 dim GLFSR As UShort = 1 Public Function GetRNDushort() As UShort 'LCG LCG=(127*LCG+259) 'Galios LFSR If (GLFSR And 1) = 1 Then GLFSR = GLFSR Xor 447 '447 is the tap value GLFSR = (GLFSR >> 1) Or &H8000 Else GLFSR = (GLFSR >> 1) End If return GLFSR Xor LCG End Function Public Sub InitializeRNDushort(ByVal ReSeed As UShort) LCG = ReSeed GLFSR = LCG Xor &H5555 'just making the start values very different - not realy important If GLFSR = 0 Then 'except that GLFSR must not be zero GLFSR=1 End If End Sub End Module
Module Three - returns Random UInteger (with option to return Max set value) :
'***************************************************************************** '* Name : RandGen.BAS * '* Author : Gavin Wiggett * '* : * '* Date : 20/05/2013 * '* Version : 1.0 * '* * '* Notes : A Rudimentary Pseudo Random Number Generator (Modulo based) * '* Usage : Call Initialize() to initialize the Initial value of the * '* : generator. The generator gives better values when the initial * '* : seed value is a Prime Number. * '* : For the same Initial Seed, you will get the same serie of * '* : generated values. This let you repeat some experiences (and * '* : this is why it's called a PSEUDO-random number generator. * '* : If you need an automatic different initial value each time you * '* : start the number generator, you can set the initial value to * '* : the read of an ADC value on a FLOATING Analog PIN of a PIC. * '* : Call Rand() to get/generate a new random value * '* : * '* : You can try to change the Magic Values to change the * '* : Pseudo-Random Number Generator Behaviour * '* : * '* : Many thanks to Ahmed Lazreg (Octal) for the original code * '***************************************************************************** Module RandGen private Const MagicA = 1103515245 private Const MagicB = 12345 Private Dim RndMax As UInteger ' Maximum number generated by the generator Private Dim Seed As UInteger '**************************************************************************** '* Name : GetRndNum() * '* Purpose : Return a new Pseudo Random Number each time called * '**************************************************************************** Public Function GetRndNum() As UInteger Seed = Seed * MagicA + MagicB return (Seed >> 16) mod RndMax End Function '**************************************************************************** '* Name : InitializeRandGen() * '* Purpose : Initialize the Random number generator * '* : The initial value could be a Value read from a Floating Analog * '* : PIC Pin. * '**************************************************************************** Public Sub InitializeRandGen(ByVal InitialSeed As UInteger, optional pRndMax As UInteger = 255) RndMax = pRndMax Seed = InitialSeed End Sub '**************************************************************************** '* Name : SetRndMax() * '* Purpose : Sets the Max Value that the Random number gen can generate * '**************************************************************************** Public Sub SetRndMax(ByVal pRndMax As UInteger) RndMax = pRndMax End Sub End Module