• Home
  • Videos
  • Get a Random Value from a JavaScript Array

Get a Random Value from a JavaScript Array

How to use the global Math object to retrieve a random array value from a JavaScript array. We first generate a random integer between 0 (inclusive) and the length of the array (exclusive). Then we can use that random integer to get a value from the array.

Transcript

Sometimes when we’re programming, it’s helpful to be able to select a random item out of a list of other items. In order to do that, we need to try to figure out first how can we select a random integer. So there’s a couple pieces that we need to build up this functionality. The first one is from the global math object there is a method called random and random will return a pseudo random number between 0 and less than 1. It cannot return 1 to us, it can return a floating point number up to 1. There’s no way for us to pick the initial seed that the algorithm is going to use. So basically what that means is that the randomness of the algorithm, is is pseudorandom okay, it’s not a truly random generator and we’re not going to be able to get replicable results by providing an initial seed. So every time we call Math.random we’ll get a pseudorandom number and we don’t have the ability to get the same random number every time. But for our purposes this functionality of a random number between 0 and less than 1 is going to be the the main piece that we need. So the next function we need to look at is also on the Math object. There’s a method called floor and what floor will do is it’ll return the largest integer less than or equal to the given number. In this case if we pass in a number that is an integer it will return that same integer back. If there’s any fractional component to that integer, it’ll be dropped from the integer and it’ll return back just the whole integer piece. So from there we can build this function randomInt, okay, and so what we want to do is we want to return a random integer from 0 to the limit but not including the limit itself. Okay, so in this case if we passed in 10, this would return 0 to 9. So what we want to do is we will return Math.floor because we know we need an integer. And then what we’re going to do is we’re going to do Math.random to give us some fractional component between 0 and up to not including 1. Then we’re going to multiply that by the limit. Now one thing we’re going to do just to make sure that the value that’s passed into this function is just to make sure that it’s also an integer we’re going to wrap this in Math.floor as well, that way if we call this function with 1.5, for example, you know the function will will perform correctly and we’ll get anywhere from zero to one. Okay, so now with this random integer function what we’re going to do is we’re going to consider this list of lowercase characters. Okay, this array and we’re going to try to randomly select an item from the array. So that’s pretty easy here, because all we have to do is use our random integer function and we can return the choices that we’re given, and we since we can look at the length of the array that we’re given and we can select up to that length. Right, because recall, that for an array the indexing starts at zero. So if we look at the indexes for lower case, it would be one or zero one 3 and so forth. So the fact that our random int gives us a number from 0 to the limit which would be our array length but doesn’t include the array length, that’s perfect for what we’re trying to do. So we can say choices and then we can index it like this. We say var index that we’re going to choose is equal to random integer of choices.length. Now that works because it’ll never actually include the length and so you know if we had an array of just four items the length would be four but because the index starts at zero four would be an undefined index. So because this is zero to the limit but doesn’t include the limit this is guaranteed to never give us an index that is out of bounds of the array. So you know this random integer is always going to be a valid index into the array that we’re passing in. So with that index we can say choices index and we can return that and so this this function now will work to give us a random item from any array that we pass in.

Now let’s use this as an example to build up a string of random characters so we’ll start by declaring this global variable string as an empty string and we’re just going to build a random string of five characters. Okay so in order to do that we’re going to say string += and what that’s going to do is every character that we’re about to select is going to append that onto the string one after the other so if we were randomly selecting a every time we would say and each run through this loop would would continue to just append onto the string. This is just another way of saying string equals string plus something. Okay so we’re going to say string plus equals and then we’re going to use random and we’re going to pass in our choices which is lowercase characters and so this is now going to give us a build up a lowercase string of five just alphabet characters.

That’s it we can we can use the same process to select from a list of anything as long as there are no gaps in this array. Okay now when we talk about that we’ll say array with gaps. You know, if we had some array that that had some undefined values in the middle of it our algorithm here would not work because we are not we’re not checking to make sure that this array has any missing indexes. We’re making the assumption here that with our random function that this array um you know is is not a sparse array. Okay and that’s it, so we can continue this algorithm for all kinds of things this is very helpful for things like building games or selecting characters to build up a random string lots of different use cases.