Created
March 16, 2015 20:01
-
-
Save danielbenmergui/f80c0bcfc5561ea30d1e to your computer and use it in GitHub Desktop.
Pseudorandom Numbers Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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