javascript - 使用 math.random 函数的多个输出
问题描述
因此,我使用此代码发送一条消息,该消息将聊天中发送的表情随机化(不和谐),但我想这样做,因此当发送表情时,每个表情都是不同的。例如,如果用户要发送它会发送的命令:1、2、3、4、2、3 等。我怎样才能使每个表情都不同。我发现的唯一方法是让每个表情都有不同的 var math.random 函数。这是任何其他方式,因为制作不同的 var 有点长。
const randomemote = [
`:four:`,
`:one:`,
`:two:`,
`:three:`
];
var emotes = randomemote[Math.floor(Math.random()*randomemote.length)];
message.channel.send(`
${emotes}${emotes}${emotes}${emotes}${emotes}
${emotes}${emotes}${emotes}${emotes}${emotes}
${emotes}${emotes}${emotes}${emotes}${emotes}
${emotes}${emotes}${emotes}${emotes}${emotes}
${emotes}${emotes}${emotes}${emotes}${emotes}
`)
解决方案
创建一个函数,在给定重复次数的情况下,该函数会产生多次随机表情。
const randomemote = [
`:four:`,
`:one:`,
`:two:`,
`:three:`
];
const randEmote = () => randomemote[Math.floor(Math.random()*randomemote.length)];
const multRandEmotes = count => Array.from({ length: count }, randEmote)
.join('');
const strToSend = `
${multRandEmotes(5)}
${multRandEmotes(5)}
${multRandEmotes(5)}
${multRandEmotes(5)}
${multRandEmotes(5)}
`;
console.log(strToSend);
您还可以创建另一个函数,根据要打印的行数和每行的表情数,multRandEmotes
多次调用:
const randomemote = [
`:four:`,
`:one:`,
`:two:`,
`:three:`
];
const randEmote = () => randomemote[Math.floor(Math.random()*randomemote.length)];
const multRandEmotes = count => Array.from({ length: count }, randEmote)
.join('');
const multiLineEmotes = (lines, count) => `
${
Array.from({ length: lines }, () => multRandEmotes(count))
.join('\n ')}
`;
console.log(multiLineEmotes(5, 5));
console.log(multiLineEmotes(2, 7));
推荐阅读
- python - Django过滤ForeignKey
- python - Django:有没有办法让 Heroku 管理 js 和 css 文件,但让图像来自 S3 存储桶?
- reactjs - 如何将类型分配给道具?
- sql - 今年需要吸引具有特定活动的客户(潜在客户),而该活动(潜在客户)是他们的最后一个活动
- c# - Service Fabric 节点问题
- python - 交互式股票图表,带键盘箭头的分步动画,使用 Matplolib
- sql - 未找到公开数据
- python - Plotly:如何重命名 plotly express 堆积条形图的图例元素?
- r - 在 R 中通过 biocManager 安装 msa 包时遇到问题
- java - InputStreamResource 使堆内存满并杀死 K8 容器