I have a method that accepts as argument an array of integers and I'd just change arbitrary the order of its values
public static int[] _game_number = new int[4];
public static int[] _current_number = new int[4];
public static void GetRandomTwentyFour()
{
HashSet<int> nums = new HashSet<int>();
for (int i = 0; i < 4; i++)
{
Random r = new Random();
nums.Add(r.Next(0, 4));
}
List<int> liste = nums.ToList<int>();
_current_number = new int[] { _game_number[liste[0]], _game_number[liste[1]], _game_number[liste[2]], _game_number[liste[3]] };
}
The problem is that the nums
's elements count is not always four.
- So, How can I modify my snippet to accomplish my task?
- Is there another way to do this?
If you want to reorder them randomly, simply use OrderBy with a random thing as the selector.
Usually you can use Random.Next()
But a new
Guid
would work here too², and save you a private field :²Please note that Guids are not made to be "random", but to be "unique". However, as you're probably not making an extremely official application (banking, online poker, etc.) it's fair to use it IMO. For an application where the randomness is very important, the RandomNumberGenerator from Cryptography namespace would be a better bet.
What about extension method?
Usage:
Well, just change the
4
to a variable holding the number of elements.While you're at it, you can organize things a bit. Have your
GetRandom...
method receive the array, check its size (instead of assuming it's 4) and either shuffling it in place if it's something you want, or returning a new array with the same size.You can do something like this
Here is the proof