mercredi 19 août 2020

Randomize list with seed

I'm looking for a way to randomize a List using a seed. What I want to achieve by this is controlled randomization. In other words to make sure that a list is always randomized in the same order if I use the same seed to do it.

I'm currently using this code:

string arbitrarySeed = "someValue";
System.Random random = new System.Random(BitConverter.ToInt32(Encoding.ASCII.GetBytes(arbitrarySeed), 0));
List<object> randomizedOptions = items?.OrderBy(o => random.Next()).ToList();

Which seems a bit overcomplicated so I'm looking for a more elegant way to handle this.

I know that randomizing is this way is not actually very random, but I don't need it to be true randomness. Something that comes close will do fine.




Aucun commentaire:

Enregistrer un commentaire