Giter VIP home page Giter VIP logo

hxrandom's Introduction

Random (Haxe Library)

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.

Install

haxelib install random

And then in your project's hxml build file, add

-lib random

Usage

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
	}
}

The methods it provides:

  • Random.bool()
    Will return a random true or false boolean value.
  • Random.int(from, to)
    Will generate a random integer between from and to, inclusive.
  • Random.float(from, to)
    Will generate a random float between from and to, inclusive.
  • Random.string(length:Int, ?charactersToUse:String)
    Will return a random string using of a certain length, using characters from "charactersToUse" or else A-Za-z0-9
  • Random.date(earliest:Date, latest:Date)
    Will generate a random Date object (date & time) between earliest and latest, inclusive.
  • Random.fromArray(arr)
    Will return a random item from the set array, or Null if the array is empty / null.
  • Random.enumConstructor(enum)
    Will return a random constructor from an enum, or Null if the enum has no constructors. Constructors that require parameters will be ignored.

This uses 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.

Future

If there's interest, I could add some things like:

  • Random.day() - returns a weekday (English)
  • Random.month() - returns a month (English)
  • Random.word() - returns a random word from Lorem Ipsum.
  • Random.sentence() - returns a random sentence from Lorem Ipsum.
  • Random.paragraph() - returns a random paragraph of Lorem Ipsum.
  • Random.color() - returns a random colour.

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.

Making Changes

  • Edit src/Random.hx
  • Add unit tests to test/RandomTest.hx
  • haxelib run munit t - run unit tests, check all are passing
  • haxelib run mlib v - increment version number
  • haxelib run mlib submit - submit to Haxelib

hxrandom's People

Contributors

jasononeil avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.