Is there a simple way to make a random selection f

2019-01-24 02:10发布

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>

8条回答
乱世女痞
2楼-- · 2019-01-24 02:28

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
查看更多
仙女界的扛把子
3楼-- · 2019-01-24 02:36

Here you go

function randomChoice(arr) {
    return arr[Math.floor(arr.length * Math.random())];
}
查看更多
The star\"
4楼-- · 2019-01-24 02:38

Math.random and similar functions usually return a number between 0 and 1. As such, if you multiply the random number by your highest possible value N, you'll end up with a random number between 0 and N.

查看更多
The star\"
5楼-- · 2019-01-24 02:43

JavaScript

No. Vanilla JS does not provide any method like the following. You have to calculate, unfortunately.

function sample(array) {
  return array[Math.floor(Math.random() * array.length)];
}

console.log(sample([1, 2, 3]));
console.log(sample([11, 22.3, "33", {"a": 44}]));

Try it out here.

But, if you are using lodash, the above method is already covered.

let _ = require('lodash');

console.log(_.sample([11, 22.3, "33", {"a": 44}]));

Try it out here.

Python

import random
random.choice([1, 2.3, '3'])

Try it out here.

Ruby

using a single data type,

[1, 2, 3].sample

using multiple data types,

[1, 2.34, 'a', "b c", [5, 6]].sample

Try it out here.

Updated: JavaScript example added.

查看更多
叼着烟拽天下
6楼-- · 2019-01-24 02:46
var choiceIndex = Math.floor(Math.random() * yourArray.length)
查看更多
等我变得足够好
7楼-- · 2019-01-24 02:47

Popular Underscore javascript library provides function for this, which can be used similar to python's random.choice :

http://underscorejs.org/#sample

var random_sample = _.sample([1, 2, 3, 4, 5, 6]);
查看更多
登录 后发表回答