문제

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());
         ...
     }   
  }
}
도움이 되었습니까?

해결책

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.

다른 팁

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().

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top