Random number for iterating through an array (Guided Project “Plan a Petting Zoo Visit” in Microsoft C# Foundation Course Part 5)

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.

For this exercise, you don’t need to worry about that. random.Next(i, pettingZoo.Length) is to ensure that the numbers that are less than i will be excluded for the next random number. If you don’t have i as the first parameter, sometimes it still works because it’s randomly chosen. If you run more times, possibly you would see the result described in the course.

The Random class has different implementations in .NET Framework and .NET Core. See here:

In .NET Framework, the default seed value is time-dependent. In .NET Core, the default seed value is produced by the thread-static, pseudo-random number generator.

The Random class is not completely random but should be sufficient for most scenarios. To create cryptographically strong random values, you need to use RandomNumberGenerator.

Many thanks for taking the time to answer my question. I’m sure, the implementation we use for this example is more than enough random for our case here. Honestly, what makes me a bit nervous is the fact, that I now ran this case with both version about a hundred times and I encountered not a single case in which I got a double entry or an unswapped one with the first version that should eventually produce such a result. It is almost impossible, that this is the case, if the used algorithm is anything near random. Be it like it is, gonna go on from it now to finish the course this weekend, but I will take a deep dive into randon mumbers at a later time. Thanks again and till another time.

1 Like