C
C#12mo ago
LeNoX

Getting a random number from 1 to 10

Hey, I am new to c sharp and I was wondering how can I get a random number from 1 to 10
8 Replies
Angius
Angius12mo ago
Random.Shared.Next() preferably
LeNoX
LeNoXOP12mo ago
I did it like that:
Random random = new Random();
int randomNumber = random.Next(1, 10);
Random random = new Random();
int randomNumber = random.Next(1, 10);
ty
Angius
Angius12mo ago
You can do it like that, sure Or just
int randomNumber = Random.Shared.Next(1, 10);
int randomNumber = Random.Shared.Next(1, 10);
LeNoX
LeNoXOP12mo ago
Is there any chance to convert the int to a string? Just realized you cant put both in an if statement
Angius
Angius12mo ago
.ToString()
LeNoX
LeNoXOP12mo ago
Ty, works good
ThatDaniel
ThatDaniel12mo ago
just fyi that will get you 1-9 as the second parameter is exclusive if you actually want to get 10 as a possibility then you'd need Random.Shared.Next(1, 11);

Did you find this page helpful?