Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all the number between two numbers random

Tags:

c#

random

shuffle

I'm not sure, if this question is unique, but I couldn't find the answer.

I want a good way to get numbers between 1 to 9 (including 9) randomly in C#, and I want to have all the 9 numbers. So I need a function that returns 9 numbers between 1 to 9 and I need every number exactly once.

for example, the result would look like this: 4,3,2,6,9,7,1,5,8

like image 330
Gabor Avatar asked Oct 22 '25 20:10

Gabor


1 Answers

I would just do this:

var rnd = new Random();

var numbers =
    Enumerable
        .Range(1, 9)
        .OrderBy(x => rnd.Next())
        .ToArray();

An example result I got was:

example result

like image 106
Enigmativity Avatar answered Oct 24 '25 10:10

Enigmativity



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!