有没有一种简单的方法可以从 JavaScript 或任何其他编程语言中的数组中进行随机选择? [英] Is there a simple way to make a random selection from an array in JavaScript or any other programming language?

查看:10
本文介绍了有没有一种简单的方法可以从 JavaScript 或任何其他编程语言中的数组中进行随机选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读一本初学者的 JavaScript 书籍,其中包含一些代码,这些代码将编码器的输入 (var answer) 与从数组中随机选择的字符串 (answers) 进行比较.这是一个猜谜游戏.

我对随机选择字符串的方式感到困惑.该代码似乎是将 Math.random 函数乘以 answers 数组及其长度属性.检查一下,这似乎是从数组中随机选择的标准方法?为什么要使用数学运算符 * 来乘...出...基于数组长度的随机字符串?从技术上讲,长度不是只有 3 个字符串吗?我只是觉得它应该是一些简单的东西索引 = answers.random.这在 JS 或其他语言中是否存在?

解决方案

Python 很简单.

<预><代码>>>>随机导入>>>random.choice(['red','green','blue'])'绿色'

您正在查看的代码如此常见的原因是,通常,当您谈论统计中的随机变量时,它的范围是 [0,1).如果您愿意,可以将其视为百分比.为了使这个百分比适合选择一个随机元素,你将它乘以范围,允许新值介于 [0,RANGE) 之间.Math.floor() 确保数字是整数,因为小数在用作数组索引时没有意义.

您可以使用自己的代码轻松地在 Javascript 中编写一个类似的函数,而且我确信有很多 JS 实用程序库都包含一个.类似的东西

函数选择(选择){var index = Math.floor(Math.random() * selection.length);返回选择[索引];}

然后您可以简单地编写 choose(answers) 以获得随机颜色.

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>

解决方案

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.

这篇关于有没有一种简单的方法可以从 JavaScript 或任何其他编程语言中的数组中进行随机选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆