JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 11
  • Score
    100M100P100Q49460F
  • License MIT

Tools for generating different types of random behaviour in Javascript

Package Exports

  • chancer

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (chancer) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Chancer

Tools for generating different types of random behaviour in Javascript.

NPM version Node.js version support MIT licensed Code coverage Build status

Getting Started

You can use Chancer on the server side with Node.js and npm:

npm install chancer

On the client side, you can include Chancer in your page (found in build/chancer.js):

<script src="chancer.js"></script>

Usage

In Node.js you can include Chancer in your script by using require:

var chancer = require('chancer');

If you're including via a <script> tag, chancer is available as a global variable.

chancer.random()

Generates a floating-point number between 0 and 1
return: (float) Returns the floating point number

chancer.random(); // 0.32831766246818006

chancer.float( min, max )

Generates a floating-point number between min (inclusive) and max (exclusive)
min: (number) Minimum inclusive number
max: (number) Maximum exclusive number
return: (float) Returns the floating point number

chancer.float(1, 10); // 2.794354454614222

chancer.int( min, max )

Generates an integer between min (inclusive) and max (inclusive)
min: (number) Minimum inclusive number
max: (number) Maximum inclusive number
return: (integer) Returns the integer number

chancer.int(1, 10); // 3

chancer.coinToss( heads, tails )

Generates a 0 or 1 to represent a coin toss
Optionally associate any other values for the coin sides (true/false, yes/no, heads/tails, etc)
heads: Value for the head
tails: Value for the tails
return: Returns the chosen value

chancer.coinToss(); // 1
chancer.coinToss(true, false); // true
chancer.coinToss('heads', 'tails'); // tails

chancer.fromArray( arr )

Generates a random item from an array
arr: (array) Array of items
return: (integer) Returns the randomly selected item

chancer.fromArray([ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]); // 6

chancer.shuffleArray( arr )

Generates an array shuffled into a random order
arr: (array) Array of items
return: (array) Returns the array shuffled into a random order

chancer.shuffleArray([ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]); // [7, 3, 9, 4, 1, 6, 2, 5, 8]

chancer.fillArray( min, max, total )

Generates an array of integers between min (inclusive) and max (inclusive)
If no total specified return all possible values between min and max
min: (number) Minimum inclusive number
max: (number) Maximum inclusive number
total: (number) Total number of results | all
return: (array) Returns the chosen values

chancer.fillArray(1,10); // [3, 5, 4, 1, 6, 7, 8, 2, 10, 9]
chancer.fillArray(1,100,10); // [40, 66, 74, 39, 17, 99, 50, 70, 6, 15]

chancer.uuid()

Generates a random value as a universally unique identifier (UUID) version 4 (RFC4122)
return: (string) Returns string representation of a UUIDv4

chancer.uuid(); // 73f765b2-958a-45fb-be10-b9fcebdd6838

Browser Support

Tested in the following browsers

Chrome Opera Firefox Safari IE iOS Android WindowsPhone
chrome Opera firefox safari ie 3+ 2.2+ 8.1+

License

Chancer is licensed under the MIT license.
Copyright © 2015, Alex Kilgour