0

I was wondering if there is a way to generate the same semi random number on two machines at the same time. Here is an example of how I am currently getting my randoms.

var randomValue = generateRandom( -200, 200 );

function generateRandom( min, max ) 
{
    min = Math.ceil( min );
    max = Math.floor( max );
    return Math.floor( Math.random() * ( max - min + 1 ) + min );
}

What I can do is send message between the two devices. I don't know if that will help because randomValue is being called within a function. I was thinking maybe we can use a timer or something to generate the value on both machines.

Amit
  • 45,440
  • 9
  • 78
  • 110
Kahless
  • 1,213
  • 2
  • 13
  • 31
  • 4
    If you can communicate between the machines, why do you need to generate the number on both places? Do it once and share... (And no, built-in pseudo random generator has no seeding function so you can't synchronize it between machines) – Amit Jan 01 '16 at 18:42
  • So I'm worried that the function that is calling randomValue will need the value before the message is received. – Kahless Jan 01 '16 at 18:44
  • Generate a collection of numbers ahead of time then – Amit Jan 01 '16 at 18:45
  • I was thinking about that but the parameters change on the fly – Kahless Jan 01 '16 at 18:46
  • Pick an [RNG algorithm](https://en.wikipedia.org/wiki/List_of_random_number_generators), and give it the same seed. – SLaks Jan 01 '16 at 18:47
  • What parameters? The pseudo random generator is parameterless – Amit Jan 01 '16 at 18:48
  • Sorry bad choice of words. The min, max values will being changing. – Kahless Jan 01 '16 at 18:49
  • RNG algorithm sounds interesting. What would you recommend for the seed and how would I go about making sure they both get the same value. – Kahless Jan 01 '16 at 18:52

3 Answers3

2

Generate a list of random values between 0 & 1 using the built-in generator (Math.random()) ahead of time on one of the machines ("master"), share it with the slave(s).

Wherever you need a concrete value, use the next number in that list with the current parameters (min-max range) and you'll have identical numbers on all machines.

If you "run out" of values, generate and transmit another batch (again, ahead of time).

Amit
  • 45,440
  • 9
  • 78
  • 110
  • This sounds very simple and effective. I can generate the array of random numbers and pass it to the slaves but I'm not sure how I to "use the next number in that list with the current parameters (min-max range) " . Can you alter the example above to demonstrate that? – Kahless Jan 01 '16 at 19:01
  • Ooooo I see. Just replace Math.random() with the array value. Stupid question. Thanks for the help. – Kahless Jan 01 '16 at 19:08
  • @JohnBob The example above is incomplete and missing the communication part, but if all machines (master & slaves) have the same array of values, and all use up the same value every time, then all machines know the next value. Basically this means you need to replace `Math.random()` with something like `randomValues.pop()` or `randomValues.shift()`. – Amit Jan 01 '16 at 19:10
0

How about using 'seedable' random number generator, which is discussed in Seedable JavaScript random number generator ?

And there is an implementation on github : https://github.com/davidbau/seedrandom

Community
  • 1
  • 1
0

To genrate a same identical number on two machines at the same time.

  1. Genrate the current timestamp new Date().getTime()
  2. Reverse this number (This will sufficiently be random everytime)
  3. Get it in the range of (-200,200) by devision and multiplication with constants.

This number will be identical on two machines at a given time.

void
  • 36,090
  • 8
  • 62
  • 107