RosettaCodeData/Task/Linear-congruential-generator/FreeBASIC/linear-congruential-generat...

58 lines
1.2 KiB
Plaintext

' version 04-11-2016
' compile with: fbc -s console
' to seed BSD_lcg(seed > -1)
' to get random number BSD_lcg(-1) or BSD_lcg() or just BSD_lcg
Function BSD_lcg(seed As UInteger = -1) As UInteger
Static As UInteger bsd_state
If seed <> -1 Then
bsd_state = seed Mod 2 ^ 31
Else
bsd_state = (1103515245 * bsd_state + 12345) Mod 2 ^ 31
End If
Return bsd_state
End Function
' to seed ms_lcg(seed > -1)
' to get random number ms_lcg(-1) or ms_lcg() or just ms_lcg
Function ms_lcg(seed As Integer = -1) As UInteger
Static As UInteger ms_state
If seed <> -1 Then
ms_state = seed Mod 2 ^ 31
Else
ms_state = (214013 * ms_state + 2531011) Mod 2 ^ 31
End If
Return ms_state Shr 16
End Function
' ------=< MAIN >=------
Dim As Long i
Print "MS generator"
' ms_lcg(0) ' state = 0 at the start of the program
For i = 1 To 10
Print Using "###########"; ms_lcg
Next
Print
Print "BSD generator"
' BSD_lcg(0) ' state = 0 at the start of the program
For i = 1 To 10
Print Using "###########"; BSD_lcg
Next
' empty keyboard buffer
While InKey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End