How to select three unique numbers between 1-10 at random

I have a workflow that requires selecting three random numbers from 1-10 but the cannot be the same. I’m familiar with New Random().Next(1,10), but they are occasionally the same.

I’m thinking I can create an array the includes each number,
Generate random number between 1-10, select and remove that number from the array
Generate a random number between 1-9, select and remove that number from the array
Generate a random number between 1-8, select and remove that number from the array

So:
1,2,3,4,5,6,7,8,9,10
Generate random between 1-10: 6
6 is chosen
1,2,3,4,5,7,8,9,10
Generate random between 1-9: 6
7 is chosen
etc

A) how would I go about doing this?
B) this seems suboptimal. Is there a better way?

Hi @riley , here’s my solution:

Hi,

How about the following?

arrInt = {1,2,3,4,5,6,7,8,9,10}.OrderBy(Function(i) New Random().Next).ToArray()

Then

res = arrInt.Take(3).ToArray

note: res is array of int32

Regards,

@riley

You can try this

randomarray = Enumerable.Range(1,10).OrderBy(function(x) guid.newguid()).Take(3).ToArray

Randomarray is array of int32

Cheers

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.