GithubHelp home page GithubHelp logo

acardinale / weighted-shuffle Goto Github PK

View Code? Open in Web Editor NEW

This project forked from denizdogan/weighted-shuffle

0.0 1.0 0.0 334 KB

Shuffle a collection of weighted values

License: Apache License 2.0

JavaScript 8.69% TypeScript 91.31%

weighted-shuffle's Introduction

weighted-shuffle

Perform a non-destructive weighted shuffle on a collection. Based on the algorithms described in Weighted Random Sampling (2005; Efraimidis, Spirakis).

Installation

$ npm i weighted-shuffle

TypeScript declarations are included.

Usage

The package exports one function, shuffle, which takes two arguments. The first argument is the input collection, which is an object on the form {value: weight, ...}, or an array of tuples on the form [[value, weight], ...]. The second argument is optional and either 'asc' or 'desc'. See below for examples.

The weight must always be a number. If it's not a number, the behavior of this library is undefined.

Since object property keys must be strings in JavaScipt, the same applies to this package.

In the array-tuple form, the value can be anything. The output is always an array of tuples on the form [value, weight] where weight is a value between 0 and 1 based on the input weight.

Examples

$ node
> const { shuffle } = require('weighted-shuffle')
undefined
> shuffle({ a: 1, b: 2, c: 3, d: 4 })
[ [ 'a', 0.3332369734469953 ],
  [ 'd', 0.4435102443860357 ],
  [ 'b', 0.634610481335515 ],
  [ 'c', 0.6893187374490146 ] ]
> shuffle([['a', 1], ['b', 2], ['c', 3], ['d', 4]])
[ [ 'a', 0.17752200849829158 ],
  [ 'd', 0.4915930588445954 ],
  [ 'b', 0.5257541251818763 ],
  [ 'c', 0.8857869178489653 ] ]
> shuffle({ a: 1, b: 2, c: 3, d: 4 }, 'desc')
[ [ 'b', 0.8705423064592988 ],
  [ 'c', 0.729363245199407 ],
  [ 'd', 0.6693826180657991 ],
  [ 'a', 0.25133205081259735 ] ]

Contributing

  • Make a PR!

weighted-shuffle's People

Contributors

benjlevesque avatar denizdogan avatar dependabot[bot] avatar

Watchers

 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.