Eric
Eric

Reputation: 10646

Procedural functions with seed (like road generator)

I've searched around but didn't find a solid answer. I'm building a game in AS3. I have no issue generating random map (2D tiles) for my game, which is basically made of random numbers. How do I obtain the exact same result all the time passing a seed parameter to my function ?

function generate(__xt:uint, __yt:uint){
  var rnd:int;
  for (var i:uint=0; i < __xt; i++){
     for(var j:uint=0; j < __yt; j++){
         rnd = Math.round(Math.random());
         ...
     }   
  }
}

Upvotes: 0

Views: 686

Answers (2)

Nicolas Siver
Nicolas Siver

Reputation: 2885

Grant Skinner developed great library for generating random numbers, I would recommend to use it, not Math.random()

... I decided to build a class to generate random numbers based on a seed number. This is also handy for other uses like statistics, testing, and game development (ex. synchronizing or replaying game play). It was quite straightforward, because Flash Player already has a mechanism for generating a series of random numbers based on a seed hidden away in its APIs â BitmapData.noise().

Upvotes: 1

fsbmain
fsbmain

Reputation: 5267

Yes Math.random() doesn't support seeding and if you need it, you have to implement your own PRNG. Take a look in this answer Seedable JavaScript random number generator for the JS, it's quite easy to port one of the answers to as3.

Upvotes: 2

Related Questions