Generate a random number from another number

Is it possible in JavaScript to generate a random number from another number?

I am trying to implement a predictable random number generator for one of my fractal terrain generators. I already know that you can create a random number using Math.random (), but I want to create a random number generator that produces exactly one output for each input. (For example, predictableRandomGenerator(1) will always produce the same result, which will not necessarily be the same as the input.)

So, is it possible to generate a random number from another number, where the output is always the same for each input?

+6
source share
5 answers

You can use a checksum generator, such as MD5 or SHA-1, to generate a single pseudo-random output for each input. SHA-1 will generate a random number from each line that is input as an input, and each output will produce exactly one input. (Probably any other checksum generator would be suitable for this purpose, since the checksum generators produce exactly one output for each input entered).

+6
source

Yes it is possible. However, you need to write your own pseudo random number generator.

See, computers cannot generate random numbers. However, you can use an algorithm that creates a sequence of numbers that seems random.

This algorithm usually receives a seed, and each seed leads to a different sequence of random numbers generated by the algorithm.

The most common algorithm is a linear congruent pseudorandom number generator, as defined by D. Kh. Lemer and described by Donald E. Knut in the Art of Computer Programming, Volume 2: “Seven-Dimensional Algorithms”, Section 3.2.1.

See the following thread for more details: Predict Javascript's Math.random seed

+4
source

Of course, what about the identification function:

 function getMappedRandom(random){ return random; } 

I'm not sure why you need this transformation, but from the point of view of randomness, this does not necessarily make it better.

Random number generator

+3
source

I believe that what you need is called a one-way hash function. try hex_md5 () or hex_sha1 ().

+2
source

If you need a PRNG for a terrain generator, then I guess you need a seeding random number generator that reproduces a sequence of pseudorandom numbers for a given seed; Thus, each seed creates a separate, separate landscape, which can be reconstructed later by providing the same seed.

This might work for you:

http://davidbau.com/archives/2010/01/30/random_seeds_coded_hints_and_quintillions.html http://davidbau.com/encode/seedrandom.js

0
source

Source: https://habr.com/ru/post/945819/


All Articles