Home made Random Function?

Blitz3D Forums/Blitz3D Programming/Home made Random Function?

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

A simple solution, definitely not the best, is the following which I have used on occasion -


create a file called 'random.txt'

decide what precision you want with your random numbers, perhaps a byte (0-255) for instance,

write a few thousand values from 0-255 generated at random and store in your file 'random.txt'.

The above steps are only needed to be done once and never again after that.

In your game/app read these values, from the file, into an array, bank or type whichever suits best.

Assuming for the moment you went with 1000 values, then you can make a simple function similar to
'seedrnd millisecs()' by setting the first random number to be generated in your sequence = millisecs() mod 1000 (same value between 0-999). Make sure you store the 'seed' used if you are intending for a replay of the game or similar.

From that point, after generating your starting point simply iterate through the list of numbers, incrementing the array index by one each time and looping around when you reach the maximum.

Of course, this means that your 'random sequence' will repeat after you've gone through all the numbers in the sequence but I would think it highly unlikely it would be noticed. As long as you start from a different point in the list initially it's hardly going to matter.

As I said, not a great solution but it is easy and allows for the same simulation to take place each time if given the same seed on each person's machine since it uses the same data file as a reference.