npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

chancer

v2.0.2

Published

Tools for generating random behaviour in Javascript

Downloads

17

Readme

Chancer

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

Simple collection of tools for generating random behaviour in Javascript. Uses a Mersenne Twister for more accurate random behaviour and repeatability.

Try out the test page in your browser.

Getting Started

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

npm install chancer

you can then include Chancer in your script by using require:

var chancer = require('chancer');

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

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

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

Seeding

You can specify a particular seed in order to get a repeatable random sequence:

chancer.seed( num )

Generates a seed that will always produce the same random sequence
num: (number) Integer to use as seed

chancer.seed(123);

Methods

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