If I have a list of integers:
List<int> myValues = new List<int>(new int[] { 1, 2, 3, 4, 5, 6 } );
How would I get 3 random integers from that list?
If I have a list of integers:
List<int> myValues = new List<int>(new int[] { 1, 2, 3, 4, 5, 6 } );
How would I get 3 random integers from that list?
Use the below code to get the number:
The simplest way would be something like this:
But a better method, if you want to ensure there are no duplicates is to use a shuffling algorithm, like the Fisher-Yates algorithm, then take the first 3 items:
or this:
There are ways of doing it! A simple google can fetch you hundreds of answers. However, you can do this!
One simple way:
The better way: Fisher–Yates shuffle:
how you use it:
Combining the other answer with this answer can lead you to the following:
In this case
1
is the starting value (inclusive) and6
is the number of integers to generate.