This is the first version of a part of the solution from Microsofts guided project “Plan a Petting Zoo Visit”:
int r = random.Next(pettingZoo.Length);
They write: “Now you cycle through each element in the array, select a random index, and swap it with the current element. However, if you run this code and observe the changes on pettingZoo, you might notice an issue. Some elements don’t get swapped at all, and some elements are swapped multiple times.”
Then they tell us to change it to this in the next step. " You can improve the distribution of randomly selected elements by updating the range as you move through the array."
int r = random.Next(i, pettingZoo.Length);
Giving the following explanation: “Now, as you iterate through the for loop, you update the range of the randomly selected index to exclude values less than i. This is because indices at values less than i have already been swapped in previous iterations of the loop.”
Here, I have a question for you:
I observed, that on my environment, it does not have any effect, if I use the first or the second version of this code. I always get the animals swapped and never having any duplicates. I’m using the very latest version of the .Net SDK. Is it possible, that this version does things different, that it did at the time Microsoft wrote the course module? I also found out, that there are some things to consider, if you would need real random numbers for let’s say generating passwords or cryptographic applications. I only am asking this here, because I will need random numbers a lot in the future and I am a bit confused by this. Thanks in advance if someone can give me some information or explanation on it. Happy learning, my friends.