You know, PB's generator is pretty darn good actualy for games and stuff.
What might be needed in addition is a cryptologicaly "secure" variant
(and add it to the chiper lib).
Run this test:
Code: Select all
DisableDebugger
numbers.l=1000000
zero.l=0
one.l=0
n.l=0
i.l=0
;RandomSeed(Date()) ;optional as PB allready do this at program start,
;but this is how/where you would restore/set the seed from a saved game or similar.
start=ElapsedMilliseconds()
For i=1 To numbers
n=Random(1)
If n=1
one+1
Else
zero+1
EndIf
Next
stop=ElapsedMilliseconds()
spread.d=one/zero
text$=Str(zero)+" zeros"+#LF$
text$+Str(one)+" ones"+#LF$
percent.d=0.0
If spread>1.0
percent=100.0/spread
Else
percent=spread*100.0
EndIf
text$+StrD(percent)+" percent uniform spread"+#LF$
speed.d=(stop-start)/1000.0
text$+StrD(speed)+" seconds to generate "+Str(numbers)+" numbers"+#LF$
MessageRequester("Random() Test",text$)
If the spread is 100% that means it is perfectly uniform,
which is pretty much impossible to do without actualy keeping track
of all previous values, and no normal generators do that.
If it is anything other than 100% there is a bias,
which is why I show the count of each number so you can see the bias.
This is a standard way (as far as I could learn from info I found)
to test random generators.
Here on the test, 1 million numbers was generated,
if the generator was perfect it would result in 100% meaning
half a million 0's and half a million 1's.
PureBasic (and thus Fred actualy

) is very good.
With 1 million numbers generated the spread is over 99%
and the speed is very impressive too btw only fraction of a second.
The higher you set the numbers.l= the longer it takes to run the test obviously, 1 billion took over half a minute here,
but the spread weas even closer to 100%.
If you reduce the numbers.l= to something very low, you will find that the spread gets worse.
numbers.l=10 for example, you may actualy end up wih 0% spread
(only 0's for example), or anywhere between 0-100%.
So the more numbers/runs the the better spread you get.
Which is good, esp for a game that may need several thousand if not millions of random numbers during a game session for events etc.
A word of warning though (altough you can try).
Do not use RandomSeed(Date()) or similar right inside the loop.
You will find horrible spread of numbers.
My recommendation? Don't use RandomSeed() in the loop unless you really know what you are doing.
It would be nice with a GetRandomSeed() though as a game could then "save" the seed and then set it later (when a save game is loaded)
to ensure that the random state is consistent across save games.
Hmm. GetRandomSeed() and rename RandomSeed() to SetRandomSeed() so it matches the naming conventions of other stuff?
Anyway, Random() impress me, I wasn't aware it was as good as this.
EDIT:
For info on cryptographicaly secure random genrators here is a nice link,
http://www.ssh.fi/support/cryptography/algorithms/
http://www.itl.nist.gov/fipspubs/fip186.htm (no idea what license these algorythms has though)