Skip to content

Instantly share code, notes, and snippets.

@danielbenmergui
Created March 16, 2015 20:01
Show Gist options
  • Save danielbenmergui/f80c0bcfc5561ea30d1e to your computer and use it in GitHub Desktop.
Save danielbenmergui/f80c0bcfc5561ea30d1e to your computer and use it in GitHub Desktop.
Pseudorandom Numbers Class
package gamelib ;
class RandomGenerator
{
public var seed:String;
private var numSeed:Float;
private var curSeed:Float;
public function new(seed:String)
{
this.seed = seed;
reset();
}
public function reset():Void
{
numSeed = 0;
for(i in 0...seed.length)
{
numSeed += seed.charCodeAt(i) * Math.pow(10, i);
}
curSeed = numSeed;
}
public function int(from:Int, to:Int):Int
{
return Std.int((to - from + 1) * float() + from);
}
public function floatRange(from:Float, to:Float):Float
{
return (to - from) * float() + from;
}
public function bool():Bool
{
return float() < 0.5;
}
public function float():Float
{
curSeed = (curSeed * 16807.) % 2147483647.;
var ret = curSeed / 2147483647.;
return ret;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment