从 javascript 数组中随机选择一个项目
pick a random item from a javascript array
我正在制作一个可以回复我的消息的机器人。
如果我发送 Hi!
给机器人,它会回复 Well, hello there!
。我只是想知道,我该怎么做才能为机器人提供多种答案选择? 有没有办法使用 JavaScript 从响应数组中随机选取一个项目?
没有 JavaScript "command" 允许您这样做。但是您可以做的是随机选择一个从 0 到数组长度的整数,并获取该索引处的响应数组:
var response = responses[ parseInt( Math.random() * responses.length ) ];
更简洁的方法是:
var response = responses[ Math.random() * responses.length |0 ];
其中| 0
表示带0的bitwise-or,在这种情况下只是将一个浮点数(Math.random()
returns值从0到1)转换成它最小整数
使用 Math.random * 数组的长度(向下舍入)作为数组的索引。
像这样:
var answers = [
"Hey",
"Howdy",
"Hello There",
"Wotcha",
"Alright gov'nor"
]
var randomAnswer = answers[Math.floor(Math.random() * answers.length)];
console.log(randomAnswer);
您首先需要一系列可能的响应。像这样:
var responses = ["Well hello there!","Hello","Hola!"];
然后您可以使用 Math.random
函数。此函数 returns a decimal < 1,因此您需要将其转换为整数。
var responses = ["Well hello there!","Hello","Hola!"];
var responseIndex = Math.floor((Math.random() * 10) + 1);
此外,使用模数 (%
) 运算符将随机数保持在数组索引的范围内:
var responses = ["Well hello there!","Hello","Hola!"];
var totalResponses = responses.length;
var responseIndex = Math.floor((Math.random() * 10) + 1) % totalResponses;
最后,在数组中查找您的随机响应:
var responses = ["Well hello there!","Hello","Hola!"];
var totalResponses = responses.length;
var responseIndex = Math.floor((Math.random() * 10) + 1) % totalResponses;
var response = responses[responseIndex];
alert(response);
可以在lodash中使用_.sample方法:
var responses = ["Well, hello there!", "Hello", "Hola", "Yo!", "What’s up?", "Hey there."];
console.log(_.sample(responses));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
我可以想到两个办法:
方法一:
- 使用Math.random()函数得到(0-1, 1之间的随机数
独家)。
- 将它乘以数组长度得到数字
之间(0-arrayLength)。
- 使用Math.floor()获取索引范围
从(0 到 arrayLength-1)。
const answers = [
"Hey",
"Howdy",
"Hello There",
"Wotcha",
"Alright gov'nor"
];
const randomlyPickedString=answers[Math.floor(Math.random() * answers.length)];
console.log(randomlyPickedString);
方法二:
- random(a, b)方法用于生成一个介于(a to b, b exclusive)之间的数。
- 采用底值将数字范围从(1 到 arrayLength)。
- 减去 1 得到从(0 到 arrayLength-1)的索引。
const answers = [
"Hey",
"Howdy",
"Hello There",
"Wotcha",
"Alright gov'nor"
] ;
const randomlyPickedString=answers[Math.floor(random(1, 5))-1];
console.log(randomlyPickedString);
为了便于理解代码,我编写了一个额外的变量(randomlyPickedString)。您也可以在没有它的情况下使用代码。
我正在制作一个可以回复我的消息的机器人。
如果我发送 Hi!
给机器人,它会回复 Well, hello there!
。我只是想知道,我该怎么做才能为机器人提供多种答案选择? 有没有办法使用 JavaScript 从响应数组中随机选取一个项目?
没有 JavaScript "command" 允许您这样做。但是您可以做的是随机选择一个从 0 到数组长度的整数,并获取该索引处的响应数组:
var response = responses[ parseInt( Math.random() * responses.length ) ];
更简洁的方法是:
var response = responses[ Math.random() * responses.length |0 ];
其中| 0
表示带0的bitwise-or,在这种情况下只是将一个浮点数(Math.random()
returns值从0到1)转换成它最小整数
使用 Math.random * 数组的长度(向下舍入)作为数组的索引。
像这样:
var answers = [
"Hey",
"Howdy",
"Hello There",
"Wotcha",
"Alright gov'nor"
]
var randomAnswer = answers[Math.floor(Math.random() * answers.length)];
console.log(randomAnswer);
您首先需要一系列可能的响应。像这样:
var responses = ["Well hello there!","Hello","Hola!"];
然后您可以使用 Math.random
函数。此函数 returns a decimal < 1,因此您需要将其转换为整数。
var responses = ["Well hello there!","Hello","Hola!"];
var responseIndex = Math.floor((Math.random() * 10) + 1);
此外,使用模数 (%
) 运算符将随机数保持在数组索引的范围内:
var responses = ["Well hello there!","Hello","Hola!"];
var totalResponses = responses.length;
var responseIndex = Math.floor((Math.random() * 10) + 1) % totalResponses;
最后,在数组中查找您的随机响应:
var responses = ["Well hello there!","Hello","Hola!"];
var totalResponses = responses.length;
var responseIndex = Math.floor((Math.random() * 10) + 1) % totalResponses;
var response = responses[responseIndex];
alert(response);
可以在lodash中使用_.sample方法:
var responses = ["Well, hello there!", "Hello", "Hola", "Yo!", "What’s up?", "Hey there."];
console.log(_.sample(responses));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
我可以想到两个办法:
方法一:
- 使用Math.random()函数得到(0-1, 1之间的随机数 独家)。
- 将它乘以数组长度得到数字 之间(0-arrayLength)。
- 使用Math.floor()获取索引范围 从(0 到 arrayLength-1)。
const answers = [ "Hey", "Howdy", "Hello There", "Wotcha", "Alright gov'nor" ]; const randomlyPickedString=answers[Math.floor(Math.random() * answers.length)]; console.log(randomlyPickedString);
方法二:
- random(a, b)方法用于生成一个介于(a to b, b exclusive)之间的数。
- 采用底值将数字范围从(1 到 arrayLength)。
- 减去 1 得到从(0 到 arrayLength-1)的索引。
const answers = [ "Hey", "Howdy", "Hello There", "Wotcha", "Alright gov'nor" ] ;
const randomlyPickedString=answers[Math.floor(random(1, 5))-1]; console.log(randomlyPickedString);
为了便于理解代码,我编写了一个额外的变量(randomlyPickedString)。您也可以在没有它的情况下使用代码。