views:

77

answers:

2

Hi All,

I was wondering if there is good implementation of Random.Next in AS-3.

Basically want to generate a series of Random numbers given a seed,and at times minumum and maximum limits..

Similar to C# System.Random class.

Random random = new Random();  
return random.Next(min, max);  

Thanks All.

+2  A: 

Here it is :

function randomNext(min:Number, max:Number):Number {
 return Math.random()*(max-min)+min;
}
Patrick
We don't have make such utility functions into the standards flash API. This can be done easily like the example above. So it keeps the library small.
jase21
This is not a seeded random number.
Joony
@Joony: it is a seeded random number. But the seeding is not exposed through the API.
back2dos
Well, yes, we're talking semantics now. Not exposing the seeding makes the number non-repeatable (essentially completely random), and therefor useless in some circumstances. The actual implementation is irrelevant if what I want is to seed a random number. I should clarify my statement: "This is not a user seeded random number." The question states "given a seed." I think the inquirer is looking for repeatable results.
Joony
+3  A: 

Grant has a random number class, and a seeded random number class:

Non seeded: http://www.gskinner.com/blog/archives/2008/01/source_code_ran.html

Seeded: http://www.gskinner.com/blog/archives/2008/01/source_code_see.html

Also, you can find a decent implementation here:

http://lab.polygonal.de/2007/04/21/a-good-pseudo-random-number-generator-prng/

Example:

package{
  import flash.display.Sprite;
  import de.polygonal.math.PM_PRNG;

  public class RandomTest extends Sprite{

    public function RandomTest(){
      var random:PM_PRNG = new PM_PRNG();
      random.seed = 1234567890;
      for(var i:uint = 0; i< 10; i++){
        trace(random.nextDouble());
      }
      trace("----------");
      random = new PM_PRNG();
      random.seed = 1234567890;
      for(i = 0; i< 10; i++){
        trace(random.nextDouble());
      }
    }
  }
}
Joony