Last active
August 29, 2015 14:17
-
-
Save ruby0x1/021f7ffad6046828e270 to your computer and use it in GitHub Desktop.
Random.hx
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 ; | |
//Adapted from code by Grant Skinner, see bottom of file. | |
//This is cut from https://github.com/underscorediscovery/luxe under MIT license | |
/** | |
Given an `initial` value for the seed, subsequent generated numbers will be predictable, | |
and the `seed` value updated to reflect the current seed which can be used to resume predictability | |
from an ongoing set. Uses a Park–Miller pseudo random number generator. | |
*/ | |
class Random { | |
public function new( _initial_seed:UInt ) { | |
seed = initial = _initial_seed; | |
} | |
//Public API | |
/** Returns a float number between [0,1) */ | |
public inline function get() : Float { | |
return (seed = (seed * 16807) % 2147483647)/0x7FFFFFFF+0.000000000233; | |
} | |
/** Returns a number between [min,max). | |
max is optional, returning a number between [0,min) */ | |
public inline function float( min:Float, ?max:Null<Float>=null ) : Float { | |
if(max == null) { max = min; min = 0; } | |
return get() * ( max - min ) + min; | |
} | |
/** Return a number between [min, max). | |
max is optional, returning a number between [0,min) */ | |
public inline function int( min:Float, ?max:Null<Float>=null ) : Int { | |
if(max == null) { max = min; min=0; } | |
return Math.floor( float(min,max) ); | |
} | |
/** Returns true or false based on a chance of [0..1] percent. | |
Given 0.5, 50% chance of true, with 0.9, 90% chance of true and so on. */ | |
public inline function bool( chance:Float = 0.5 ) : Bool { | |
return (get() < chance); | |
} | |
/** Returns 1 or -1 based on a chance of [0..1] percent. | |
Given 0.5, 50% chance of 1, with 0.9, 90% chance of 1 and so on. */ | |
public inline function sign( chance:Float = 0.5) : Int { | |
return (get() < chance) ? 1 : -1; | |
} | |
/** Returns 1 or 0 based on a chance of [0..1] percent. | |
Given 0.5, 50% chance of 1, with 0.9, 90% chance of 1 and so on. */ | |
public inline function bit( chance:Float = 0.5) : Int { | |
return (get() < chance) ? 1 : 0; | |
} | |
/** Reset the initial value to that of the current seed. */ | |
public inline function reset() { | |
var s = seed; | |
initial = s; | |
} | |
/** get the current seed (read only, change via `initial`)*/ | |
@:isVar public var seed (default,null): UInt; | |
/** get/set the initial base seed */ | |
@:isVar public var initial (default,set): UInt; | |
inline function set_initial( _initial : UInt ) { | |
initial = seed = _initial; | |
return initial; | |
} | |
} //Random | |
//Based on code from http://blog.gskinner.com/archives/2008/01/source_code_see.html | |
//With license: | |
/** | |
* Rndm by Grant Skinner. Jan 15, 2008 | |
* Visit www.gskinner.com/blog for documentation, updates and more free code. | |
* | |
* Incorporates implementation of the Park Miller (1988) "minimal standard" linear | |
* congruential pseudo-random number generator by Michael Baczynski, www.polygonal.de. | |
* (seed * 16807) % 2147483647 | |
* | |
* | |
* | |
* Copyright (c) 2008 Grant Skinner | |
* | |
* Permission is hereby granted, free of charge, to any person | |
* obtaining a copy of this software and associated documentation | |
* files (the "Software"), to deal in the Software without | |
* restriction, including without limitation the rights to use, | |
* copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the | |
* Software is furnished to do so, subject to the following | |
* conditions: | |
* | |
* The above copyright notice and this permission notice shall be | |
* included in all copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
* OTHER DEALINGS IN THE SOFTWARE. | |
*/ |
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
//once : var random = new Random(initial_seed); | |
//default seed could be : Std.int(Math.random()*0x3FFFFFFF) | |
//usage: | |
trace( random.bit() ); //0:1 | |
trace( random.bit(0.8) ); //0:1, 80% chance 1 | |
trace( random.bool() ); //true:false | |
trace( random.bool(0.8) ); //true:false, 80% true | |
trace( random.float(5) ); //[0.0 ...5.0) | |
trace( random.float(5,10) ); //[5.0 ... 10.0) | |
trace( random.initial ); //initial seed | |
trace( random.int(10) ); //[0...10) | |
trace( random.int(10,20) ); //[10...20) | |
trace( random.get() ); //[0...1) | |
trace( random.seed ); //current seed | |
trace( random.sign() ); //-1 or 1 | |
trace( random.sign(0.8) ); //-1 or 1, 80% chance 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment