Eduardo Santos de Moura Eduardo Santos de Moura - 4 years ago 205
C# Question

random.Next(0,7) ArgumentOutOfRangeException C#

I'm trying to add a dice command to my discord bot
It works perfectly if I do not specify the range -> rand.Next()
However, when I add a range (1,7), it keeps showing "Exception: ArgumentOutOfRangeException"

.Do(async (e) =>
Random rand = new Random();
int num = rand.Next(1,7);
string s = Convert.ToString(num);
await e.Channel.SendMessage(s);


edit: (1,7) instead of (0,7)

Answer Source

I don't see any problem with your arguments or your syntax. I check it like five times lol.

How about try putting the Random rand = new Random(); out of the function (I'm guessing the async setting is the one causing the problem) or better yet generate the random number outside async if possible.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download