Extremely simple helper library to easy picking random numbers in Haxe.
This extremely simple library provides simple helpers to generate random numbers in Haxe.
Note: this just uses Haxe's build in Math.random() method, it does no seeding or custom Random Number Generation. If you want something more random than Math.random
, this is not the library you're looking for.
haxelib install random
And then in your project's hxml build file, add
-lib random
import Random;
class Main
{
static function main()
{
Random.int(1,3); // 1, 2, or 3
Random.float(0,5); // Any float between 0 and 5, inclusive
Random.bool(); // True or false
Random.string(5); // A 5 character string using letters A-Z, a-z and 0-9
Random.string(10, "aeiou"); // A 10 character string using only vowels
Random.date( Date.now, nextWeek ); // Generate a random date / time between now and next week
Random.fromArray(['dog','cat','mouse']); // "dog", "cat" or "mouse"
Random.enumConstructor( Color ); // The constructors of Color, eg: Red, Blue, Green
Random.shuffle([1,2,3,4,5]); // Return the same array, but with the items shuffled
}
}
The methods it provides:
Random.bool()
true
or false
boolean value.Random.int(from, to)
from
and to
, inclusive.Random.float(from, to)
from
and to
, inclusive.Random.string(length:Int, ?charactersToUse:String)
Random.date(earliest:Date, latest:Date)
earliest
and latest
, inclusive.Random.fromArray(arr)
Random.enumConstructor(enum)
Random.shuffle(arr)
These use Haxe's Math.random(), so don't expect any fancy algorithms. It'll probably just use the platform default. It also uses 'inline' on each of it's methods, to try and keep the performance hit to a minimum. If you're really worried, write them by hand - this is just a simple helper library.
The shuffle()
method uses a variation of the Fisher Yates Shuffle algorithm.
If there's interest, I could add some things like:
As you can see, these would all be pretty useless except perhaps in a rapid prototyping context. But if there's interest, I can do it.
haxelib run munit t
- run unit tests, check all are passinghaxelib run mlib v
- increment version numberhaxelib run mlib submit
- submit to Haxelib