Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a simple way to make a random selection from an array in JavaScript or any other programming language?

I'm reading a beginner's JavaScript book with some code that compares the coder's input (var answer) to a randomly chosen string from an array (answers). It's a guessing game.

I am confused about the way a string is chosen randomly. The code appears to be multiplying the Math.random function by the answers array and its length property. Checking around, this appears to be the standard way to make a random selection from an array? Why would you use a math operator, the *, to multiply... out... a random string based on an array's length? Isn't the length technically just 3 strings? I just feel like it should be something simple like index = answers.random. Does that exist in JS or another language?

<script>  var guess = "red"; var answer = null;  var answers = [ "red", "green", "blue"];  var index = Math.floor(Math.random() * answers.length);  if (guess == answers[index]) { answer = "Yes! I was thinking " + answers[index]; } else { answer = "No. I was thinking " + answers[index]; } alert(answer);  </script> 
like image 948
nicedwar Avatar asked Jan 30 '12 22:01

nicedwar


People also ask

How do you create a random selection in JavaScript?

Use Math. random() function to get the random number between(0-1, 1 exclusive). Multiply it by the array length to get the numbers between(0-arrayLength).

How do you select an array in JavaScript?

JavaScript Array slice() The slice() method returns selected elements in an array, as a new array. The slice() method selects from a given start, up to a (not inclusive) given end. The slice() method does not change the original array.

Which is the correct way to write a JavaScript array group of answer choices?

The simplest way to create an array in JavaScript is enclosing a comma-separated list of values in square brackets ( [] ), as shown in the following syntax: var myArray = [element0, element1, ..., elementN]; Array can also be created using the Array() constructor as shown in the following syntax.


2 Answers

It's easy in Python.

>>> import random >>> random.choice(['red','green','blue']) 'green' 

The reason the code you're looking at is so common is that typically, when you're talking about a random variable in statistics, it has a range of [0,1). Think of it as a percent, if you'd like. To make this percent suitable for choosing a random element, you multiply it by the range, allowing the new value to be between [0,RANGE). The Math.floor() makes certain that the number is an integer, since decimals don't make sense when used as indices in an array.

You could easily write a similar function in Javascript using your code, and I'm sure there are plenty of JS utility libraries that include one. Something like

function choose(choices) {   var index = Math.floor(Math.random() * choices.length);   return choices[index]; } 

Then you can simply write choose(answers) to get a random color.

like image 188
Matt Luongo Avatar answered Sep 19 '22 22:09

Matt Luongo


Math.random gives you a random number between 0 and 1.

Multiplying this value by the length of your array will give you a number strictly less than the length of your array.

Calling Math.floor on that will truncate the decimal, and give you a random number within the bounds of your array

var arr = [1, 2, 3, 4, 5]; //array length = 5;  var rand = Math.random(); //rand = 0.78; rand *= arr.length; //(5) //rand = 3.9 rand = Math.floor(rand); //rand = 3 

var arr = [1, 2, 3, 4, 5]; //array length = 5;  var rand = Math.random(); //rand = 0.9999; rand *= arr.length; //(5) //rand = 4.9995 rand = Math.floor(rand); //rand = 4 - safely within the bounds of your array 
like image 38
Adam Rackis Avatar answered Sep 20 '22 22:09

Adam Rackis