I downloaded the random number generator that is supposed to generate nmbers from 1 - 5 but it doesn't do what it is supposed to
here is the code including the "rand" module
Device = 18F43K22
Clock = 16
// int osc and IO pin libraries
Include "intosc.bas"
#option DIGITALIO_INIT = true // automatically call setalldigital
Include "setdigitalio.bas"
// lcd
#option LCD_DATA = PORTD.4
#option LCD_RS = PORTD.2
#option LCD_EN = PORTD.3
Include "LCD.bas"
Include "convert.bas"
//Include "RNDByte.bas"
Include "Rand.bas"
Include "convert.bas"
Dim Y As Byte
TRISB=0
InitializeRND(4)'if desired
main:
Y=GetRND()
PORTB = (Y)
WriteAt(1,1,DecToStr(Y)," ")
DelayMS(2000)
Cls
GoTo main
{
RandGen Module
This is the Module code For the above example. Just copy And paste into the Swordfish IDE And Save in you UserLibrary folder As "RNDByte.bas"...
{
*****************************************************************************
* Name : RndByte.BAS *
* Author : David Eather *
* Notice : This code is placed into the Public Domain *
* : *
* Date : 19/07/2011 *
* Version : 1.0 *
* Notes : call InitializeRND(pValue) with a value between 0-255 *
* : call GetRND() to get a Byte 0 to 255 *
*****************************************************************************
}
{
Module Rand
Dim LCG,GLFSR As Byte
Public Function GetRND() 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 $80
Else
GLFSR = (GLFSR >> 1)
End If
result = GLFSR Xor LCG
End Function
Public Sub InitializeRND(ByVal ReSeed As Byte)
LCG = ReSeed
GLFSR = LCG Xor $55 'just making the start values very different - not realy important
If GLFSR = 0 Then 'except that GLFSR must not be zero
GLFSR=1
EndIf
End Sub
GLFSR=1
LCG=84
End