How to implement a pseudo-random function

I want to create a sequence of random numbers that will be used to select tiles for the "maze". Each maze will have an ID and I want to use that ID as a seed for a pseudo random function. This way I can create the same maze over and over, given its maze id. Preferably, I don't want to use the built-in pseudo-random function in the language as I have no control over the algorithm and it can change from platform to platform. As such, I would like to know:

  • How can I implement my own pseudo-random function?
  • Is it even possible to generate platform independent pseudo random numbers?
+3


source to share


3 answers


Yes it is possible.

Here is an example of such an algorithm (and its use) to generate noise.

These specific random functions (Noise1, Noise2, Noise3, ..) take input parameters and calculate pseudo-random values ​​from them. Their output range is 0.0 to 1.0.



And there are many others (as mentioned in the comments).

UPDATE 2019

Looking back at this answer, the one below would be a better choice mersenne twister

. Or you can find any implementation xorshift

.

+7


source


The Mersenne Twister can be a good choice for this. As you can see from the pseudocode in wikipedia, you can seed the RNG with whatever you prefer to get the same values ​​for any instance with that seed. In your case, the maze id or the hash of the maze id.



+2


source


If you are using Python, you can use a random module by entering random imports at the beginning. Then, to use it var = random.randint (1000, 9999)

This gives var a 4 digit number that can be used for its id

If you are using another language, there is probably a similar module

0


source







All Articles