Generate multiple random numbers

Asked Dec 3, 2022·2 replies·Last activity 3 years ago·Open in Discord ↗
Archived from the original community forum
Things may have changed since it was written. Still stuck? Ask the community on Discord.

Hi, I'm trying to figure out how to generate random numbers using Nightbot a certain amount of times based off the number put in after the command.

What I want to type:
!d20 4

What I want it to do:
Generate 4 random numbers between 1 and 20

I've scoured the internet and have not quite found what I need. I currently use the following code for generating a random number between 1 and 20:
$(eval Math.floor((Math.random() * 20) + 1))

I'd like to be able to generate more than one number, separated by commas, while using only one command

2 replies

Hey @Wooden Flamingo!

You can do it like so:

$(eval r = () => Math.floor(Math.random() * 20) + 1; a = parseInt(`$(1)`); !isNaN(a) ? n = a : n = 1; [...Array(n)].map(() => r()).join(', ');)

OMG thank you so much! You just saved me so much time and frustration! It works perfectly!