10 examples of 'javascript choose random from array' in JavaScript

Every line of 'javascript choose random from array' 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
92function selectRandom(arr) {
93 return arr[Math.floor(Math.random() * arr.length)];
94}
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

13function pickRandom(array) {
14 return array[Math.floor(Math.random() * array.length)];
15}
39export function randomValue(array) {
40 return array[Math.floor(Math.random() * array.length)];
41}
53function pickRandomFrom(ary) {
54 return ary[getRandomInt(0, ary.length)];
55}
23export function choose(arr) {
24 return arr[Math.floor(Math.random() * arr.length)];
25}
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}
1export function randomArrayItem(arr) {
2 return arr[Math.floor(Math.random() * arr.length)];
3}
1export default function rand(arr) {
2 return arr[Math.floor(Math.random()*arr.length)];
3}
29function randomOf(array: any[]) {
30 return array[randomInt(array.length)]
31}
51function random(from, to) {
52 return Math.floor(Math.random() * (to - from + 1) + from);
53}

Related snippets