Getting a random value from a JavaScript array
It's a simple one-liner:
const randomElement = array[Math.floor(Math.random() * array.length)];
For example:
const months = ["January", "February", "March", "April", "May", "June", "July"];
const random = Math.floor(Math.random() * months.length);
console.log(random, months[random]);
Get a random item from a JavaScript array
var item = items[Math.floor(Math.random()*items.length)];
pick a random item from a javascript array
Use Math.random * the length of the array, rounded down, as an index into the array.
Like this:
var answers = [
"Hey",
"Howdy",
"Hello There",
"Wotcha",
"Alright gov'nor"
]
var randomAnswer = answers[Math.floor(Math.random() * answers.length)];
console.log(randomAnswer);
Create an array with random values
Here's a solution that shuffles a list of unique numbers (no repeats, ever).
for (var a=[],i=0;i<40;++i) a[i]=i;
// http://stackoverflow.com/questions/962802#962890
function shuffle(array) {
var tmp, current, top = array.length;
if(top) while(--top) {
current = Math.floor(Math.random() * (top + 1));
tmp = array[current];
array[current] = array[top];
array[top] = tmp;
}
return array;
}
a = shuffle(a);
If you want to allow repeated values (which is not what the OP wanted) then look elsewhere. :)
How to get a number of random elements from an array?
Try this non-destructive (and fast) function:
function getRandom(arr, n) {
var result = new Array(n),
len = arr.length,
taken = new Array(len);
if (n > len)
throw new RangeError("getRandom: more elements taken than available");
while (n--) {
var x = Math.floor(Math.random() * len);
result[n] = arr[x in taken ? taken[x] : x];
taken[x] = --len in taken ? taken[len] : len;
}
return result;
}
Generate new random values and store in an array
Per sam's question in the comments, here's the code:
function shuffle(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
pairs = [];
for (let i = 0; i <= 3; i++) {
for (let j = 0; j <= 3; j++) {
pairs.push([i, j]);
}
}
shuffle(pairs);
pairs = pairs.slice(0, 10);
console.log(pairs);
How to retrieve a random value from an array that has values separate by a new line
What you want is to get the text from each <option>
def grabValues = scriptAll('#CarId option', '_.textContent')
This will give you an array of text values like
["Mercedes","BMW","Lexus","Honda","Toyota","VW"]
To get a random index you would use
Math.floor(Math.random() * grabValues.length)
Related Topics
How to Return Many Promises and Wait For Them All Before Doing Other Stuff
What Does "This" Refer to in Arrow Functions in Es6
Selecting Text in an Element (Akin to Highlighting With Your Mouse)
Get the Current Url With JavaScript
What's the Difference Between '$(This)' and 'This'
How to Iterate Over a JavaScript Object
Better Way to Sum a Property Value in an Array
Firebase Kicks Out Current User
Recursive Function Returns Undefined
How to Set/Unset a Cookie With Jquery
Check Variable Equality Against a List of Values
Create and Save a File With JavaScript
Is JavaScript Guaranteed to Be Single-Threaded
How to Do Case Insensitive String Comparison