lundi 22 juillet 2019

How to generate a random number using an array

I want to generate a random number between range. This is a discord bot btw. So if someone wants to generate a random number they need to write !rn min max example: !rn 1 10. The min and max numbers are going to an array called args. min is in args[0] and max is in args[1]. Now i want to make the bot send the random number using args[0] and args[1]. Here is what i tried to do:

if (msg.startsWith(prefix + 'RANDOMNUM') || msg.startsWith(prefix + 'RN')) { message.channel.send(Math.floor(Math.random() * args[0]) + args[1]); }

It checks if someone writes !rn or !randomnum, and then it's supposed to generate a random number between args[0] and args[1]. Now there are two problems. First one is if the min number is 1, the bot will send the max number the user wrote. So if i write !rn 1 5, it will send the number 5, no matter how many times you try. Second problem if the min number is not 1, it will take the min number, make it to 1, and add to it the max number. Sounds weird but here is an example: if i write !rn 4 8 it will send 18. if i write !rn 2 5 it will send 15. What am i doing wrong?




Aucun commentaire:

Enregistrer un commentaire