10 examples of 'javascript random choice' in JavaScript

Every line of 'javascript random choice' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
182function randomChoice()
183{
184 assert (
185 arguments.length > 0,
186 'must supply at least one possible choice'
187 );
188
189 var idx = randomInt(0, arguments.length - 1);
190
191 return arguments[idx];
192}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

315function pop_random(choices) {
316 // remove and return an item randomly chosen from the array of choices
317 // (mutates choices)
318 //
319 var idx = Math.floor(Math.random() * choices.length);
320 return choices.splice(idx, 1)[0];
321}
51function random(from, to) {
52 return Math.floor(Math.random() * (to - from + 1) + from);
53}
92function selectRandom(arr) {
93 return arr[Math.floor(Math.random() * arr.length)];
94}
3function choose(choices) {
4 const index = Math.floor(Math.random() * choices.length);
5 return choices[index];
6}
78public static random(): number {
79 return math.random();
80}
18function choice(somelist) {
19 var i = floor(random(somelist.length));
20 return somelist[i];
21}
23function random(params: paramsEntity = {}): number {
24 const { min = 500, max = 10000, floor = false } = params;
25 const num = Math.random() * (max - min) + min;
26 return floor ? Math.floor(num) : num;
27}
23function random(choices: { [name: string]: number }): string {
24 let summedWeight = 0;
25 const variants = [];
26 const choiceNames: Array<string> = Object.keys(choices);
27 for (const name of choiceNames) {
28 const weight = choices[name];
29 summedWeight += weight;
30 for (let i = 0; i < weight; i++) {
31 variants.push(name);
32 }
33 }
34 let seed = window.localStorage.getItem("testpilot-varianttests-id", null);
35 if (seed === null) {
36 seed = String(Math.random());
37 window.localStorage.setItem("testpilot-varianttests-id", seed);
38 }
39 return variants[Math.floor(seedrandom(seed)() * summedWeight)];
40}
19function _random(max) {
20 return Math.round(Math.random() * 1000) % max;
21}

Related snippets