{ "name": "random-weighted-choice", "version": "0.1.1", "engines": { "node": "*" }, "author": { "name": "François Parmentier" }, "main": "lib/random-weighted-choice.js", "readmeFilename": "README.md", "description": "Node.js module to make a random choice among weighted elements of table.", "dependencies": { "debug": "0.7.x" }, "devDependencies": { "mocha": "1.7.x" }, "scripts": { "test": "make test" }, "homepage": "http://github.com/parmentf/random-weighted-choice", "repository": { "type": "git", "url": "https://github.com/parmentf/random-weighted-choice.git" }, "keywords": [ "random", "weighted" ], "license": "BSD", "readme": "# Random Weighted Choice\n\n[![Build Status](https://secure.travis-ci.org/parmentf/random-weighted-choice.png?branch=master)](http://travis-ci.org/parmentf/random-weighted-choice)\n\nNode.js module to make a random choice among weighted elements of table.\n\n## Installation\n\nWith [npm](http://npmjs.org) do:\n\n $ npm install random-weighted-choice\n\n\n## Examples\n\nAlthough you can add several times the same id\n\n var rwc = require('random-weighted-choice');\n var table = [\n { weight: 1, id: \"item1\"} // Element 1\n , { weight: 1, id: \"item2\"} // Element 2\n , { weight: 4, id: \"item3\"} // Element with a 4 times likelihood\n , { weight: 2, id: \"item1\"} // Element 1, weight added with 2 => 3\n ];\n var choosenItem = rwc(table);\n var choosenUnlikely = rwc(table, 100); // The last shall be first\n var choosenDeterministically = rwc(table, 0);\n\nIt is better to not use the same twice, if you want a temperature other than\nthe default one (50).\n\n var rwc = require('random-weighted-choice');\n var table = [\n { weight: 1, id: \"item1\"} // Element 1\n , { weight: 1, id: \"item2\"} // Element 2\n , { weight: 4, id: \"item3\"} // Element with a 4 times likelihood\n , { weight: 2, id: \"item4\"} // Element 4\n , { weight: 2, id: \"item5\"}\n ];\n var choosenItem = rwc(table);\n var choosenUnlikely = rwc(table, 100); // The last shall be first\n var choosenDeterministically = rwc(table, 0);\n\nWithout temperature (second parameter) or a 50 value, likelihoods are:\n\n { item1: 10%, item2: 10%, item3: 40%, item4: 20%, item5: 20% }\n\nWith a temperature value of 100:\n\n { item1: 30%, item2: 30%, item3: 0%, item4: 20%, item5: 20% }\n\nWith a temperature value of 0, modified weights are:\n\n { item1: 0, item2: 0, item3: 8, item4: 2, item5: 2 }\n\n## Usage\n\n### random-weighted-choice(Array table, Number temperature = 50)\n\nReturn the ``id`` of the chosen item from ``table``.\n\nThe ``table`` parameter should contain an Array. Each item of that Array must\nbean object, with at least ``weight`` and ``id`` property.\n\nWeight values are relative to each other. They are integers.\n\nWhen the sum of the weight values is ``null``, ``null`` is returned (can't choose).\n\nWhen the Array is empty, ``null`` is returned.\n\nMore explanations on how it works on [Everything2](http://everything2.com/title/Blackboard+temperature).\n\n## Also\n\n* https://github.com/Schoonology/weighted", "_id": "random-weighted-choice@0.1.1", "_from": "random-weighted-choice" }