Author Topic: PJSR Win7 1171 & 1123: Math.initRandomGenerator problem  (Read 4444 times)

Offline mschuster

  • PTeam Member
  • PixInsight Jedi
  • *****
  • Posts: 1087
PJSR Win7 1171 & 1123: Math.initRandomGenerator problem
« on: 2015 October 02 17:42:34 »
Hi Juan,

Low priority on this.

On Win7 both 1171 and 1123:

I want to seed the random number generator to repeat a random sequence for testing purposes. See attempts below.

Thanks,
Mike

When I use randomSeed32 I get a runtime error. initRandomGenerator is documented to take a uint parameter but this variant does not seem to work:

Code: [Select]
   if (true) {
      var seed = Math.randomSeed32();
      console.writeln("seed: ", seed);

      var random = Math.random();
      console.writeln("random: ", random);
      var random = Math.random();
      console.writeln("random: ", random);

      Math.initRandomGenerator(seed);
      console.writeln("initRandomGenerator: ", seed);

      var random = Math.random();
      console.writeln("random: ", random);
      var random = Math.random();
      console.writeln("random: ", random);
   }

Edit: I was able to get randomSeed64 to work. I see now that multiple inits will generate the same sequence, but this sequence does not match the one following the seed call.
« Last Edit: 2015 October 02 18:33:27 by mschuster »

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: PJSR Win7 1171 & 1123: Math.initRandomGenerator problem
« Reply #1 on: 2015 October 05 02:26:49 »
Hi Mike,

Quote
I want to seed the random number generator to repeat a random sequence for testing purposes.

Simply specify a constant seed. For example:

Code: [Select]
      Math.initRandomGenerator( [3714832114, 2224669678] );
      var random = Math.random();
      console.writeln("random: ", random);
      var random = Math.random();
      console.writeln("random: ", random);

This ensures that the pseudo-random sequence is always the same on each execution.

The Math.initRandomGenerator() method has a bug because it only accepts 64-bit seeds. I have just fixed this bug in the next version; thank you for discovering it.
Juan Conejero
PixInsight Development Team
http://pixinsight.com/