Arrays are used to store data at the continuous memory location. Many people encounter a general requirement to get a random value from an array in javascript. This article will discuss different ways to get a random value from an array using various examples.
JavaScript’s Math.random() number gives a floating-point random number between 0 to less than 1 (0 is inclusive, but 1 is exclusive). This method does the initial work to generate random numbers between the desired range.
We will be using Math.random() method to get a floating-point random value between 0 to less than 1, which we can further use to generate random numbers within the range 0 to the array’s length.
Get Random Value from an Array using Math.random() and Math.floor()
Javascript’s Math.floor() method returns the largest integer less than or equal to the specified number.
Example:-
Get a random value from the array [“Harry”, “Fiona”, “Veronica”, “Sam”, “George”, “Will”]
Code:-
Frequently Asked:
- Javascript: Copy an array items to another
- Javascript: loop through an array of objects
- Javascript: Get loop counter/ index in loop
- Merge two Arrays in Javascript (3 ways)
//function to get the random value function getRandomValue(_array){ return _array[Math.floor(Math.random()*_array.length)]; } //usage of the function const myArray1 = ["Harry", "Fiona", "Veronica", "Sam", "George", "Will"]; console.log(getRandomValue(myArray1));
Output:-
Sam
Explanation:-
- Here, we created a function getRandomValue() in the above code to get a random value from an array.
- The function takes an array as the input parameter and returns any random value from that array.
- Within the function, we get a pseudo-random floating-point value between 0 to less than 1 using Math.random().
- Then multiply this value with the length of the array to get any random floating-point value within range 0 to the array’s length.
- Finally, apply Math.floor() to get the largest integer value between 0 to the array’s length.
- This value is the index position to get the corresponding array element.
- FORMULA TO GET A RANDOM VALUE WITH A RANGE:
- Math.random() * (max – min + 1 ) + min
- In our case,
- Math.random() * (5-0+1)+0 = Math.random() * 6 = Math.random() * length of the array
Similarly we can use any other integer array as well:
//function to get the random value function getRandomValue(_array){ return _array[Math.floor(Math.random()*_array.length)]; } //usage of the function const myArray2 = [5,7,9,25,35]; console.log(getRandomValue(myArray2));
Output:-
7
**Note that the output can be different for each run as there can be any random value within the array.
Get Random Value from an Array using Math.random() and ~~
Javascript’s ~~ operator coverts some of the other types to the integer type. Since it is faster, it is also used as a replacement to Math.floor()
Example:-
Get a random value from the array [“Harry”, “Fiona”, “Veronica”, “Sam”, “George”, “Will”]
Code:-
//function to get the random value function getRandomValue(_array){ return _array[~~(Math.random() * _array.length)]; } //usage of the function const myArray1 = ["Harry", "Fiona", "Veronica", "Sam", "George", "Will"]; console.log(getRandomValue(myArray1));
Output:-
Veronica
Explanation:-
- The technique is the same as the above explanation, just that we are using ~~ rather than Math.floor() method.
Similarly we can use any other integer array as well:
//function to get the random value function getRandomValue(_array){ return _array[~~(Math.random()*_array.length)]; } //usage of the function const myArray2 = [5,7,9,25,35]; console.log(getRandomValue(myArray2));
Output:-
5
Get Random Value from an Array using Math.random() and find()
Javascript’s find() method will return the first element from the array that satisfies the provided function.
Example:-
Get a random value from the array [“Harry”, “Fiona”, “Veronica”, “Sam”, “George”, “Will”]
Code:-
//function to get the random value function getRandomValue(_array) { return _array.find(( p, i, arr) => Math.random() < 1 / (arr.length - i)); } //usage of the function const myArray1 = ["Harry", "Fiona", "Veronica", "Sam", "George", "Will"]; console.log(getRandomValue(myArray1));
Output:-
Fiona
Explanation:-
- Here, we are using the find() function, assuming that the probability of selecting one random value from n items is 1/n.
I hope this article helped you get a random value in javascript. Good Luck !!!