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.
Getting Started
You can use Chancer on the server side with Node.js and npm:
npm install chancerOn 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.32831766246818006chancer.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.794354454614222chancer.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); // 3chancer.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'); // tailschancer.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 ]); // 6chancer.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-b9fcebdd6838Browser Support
Tested in the following browsers
| Chrome | Opera | Firefox | Safari | IE | iOS | Android | WindowsPhone |
|---|---|---|---|---|---|---|---|
| 3+ | 2.2+ | 8.1+ |
License
Chancer is licensed under the MIT license.
Copyright © 2015, Alex Kilgour