vendredi 5 janvier 2024

Random.Next() generates same value forever. in Paralled.Foreachasync [duplicate]

I create a Random object at the start of my method. Then in that method I run a Parallel.Foreachasync loop.

Inside this I generate 2 random numbers using Random.Next(). I then check to see if the numbers are the different in a while loop. In the while loop I generate a new random number until they are different. For the first few thousand loops this works fine. Then suddenly it gets stuck generating the same number over and over again...

var room1Index = rnd.Next(0, slot1.RoomList.Count - 1);
var room2Index = rnd.Next(0, slot2.RoomList.Count - 1);

while (room1Index == room2Index)
{
    room2Index = rnd.Next(0, slot2.RoomList.Count - 1);
}

I have checked the roomlist count and it is 4 so I would expect the two numbers to be different at least after a few iterations... However it always generates 0

Why could this be happening?




Aucun commentaire:

Enregistrer un commentaire