Math.floor(Math.random() * replies.length);不工作
Math.floor(Math.random() * replies.length); not working
const replies = [
"image 1",
"image 2",
"image 3",
"image 4",
"image 5"
];
const reply = Math.floor(Math.random() * replies.length);
const words = replies[reply]
message.channel.send(words);
所以我有这个简单的 discord 机器人代码,我应该用回复列表中的随机消息来回复,该机器人可以正常工作,但会一遍又一遍地输出相同的消息,而不是随机的(图 5)
我觉得你的代码有效!
const replies = [
"image 1",
"image 2",
"image 3",
"image 4",
"image 5"
];
const reply = Math.floor(Math.random() * replies.length);
const words = replies[reply]
console.log(words)
如果您只是控制 words
变量,这似乎可以按照您希望的方式工作。您可以在代码段中看到,每次您 运行 代码时,都会返回一张随机图像。
如@3limin4to0r 所述,如果您多次 运行 重复使用同一索引,这可能是一个问题。
const replies = [
"image 1",
"image 2",
"image 3",
"image 4",
"image 5"
];
const reply = Math.floor(Math.random() * replies.length);
const words = replies[reply]
message.channel.send(words);
所以我有这个简单的 discord 机器人代码,我应该用回复列表中的随机消息来回复,该机器人可以正常工作,但会一遍又一遍地输出相同的消息,而不是随机的(图 5)
我觉得你的代码有效!
const replies = [
"image 1",
"image 2",
"image 3",
"image 4",
"image 5"
];
const reply = Math.floor(Math.random() * replies.length);
const words = replies[reply]
console.log(words)
如果您只是控制 words
变量,这似乎可以按照您希望的方式工作。您可以在代码段中看到,每次您 运行 代码时,都会返回一张随机图像。
如@3limin4to0r 所述,如果您多次 运行 重复使用同一索引,这可能是一个问题。