-3

I need to reorder a list in a random way but I want to have the same result on a short period of time ... So I have:

var list = new String[] { "Angie", "David", "Emily", "James" }   
var shuffled = list.OrderBy(v => "4a78926c")).ToList();

But I always get the same order ... I could use Guid.NewGuid() but then I would have a different result in a short period of time.

How can I do this?

Miguel Moura
  • 36,732
  • 85
  • 259
  • 481

2 Answers2

2

Just perform a Fisher-Yates shuffle, however the random you pass in seed with a fixed value instead of allowing it to just be based on system ticks.

public static class ExtensionMethods
{
    public static void Shuffle<T>(this IList<T> list, Random rng)
    {
        int n = list.Count;
        while (n > 1)
        {
            n--;
            int k = rng.Next(n + 1);
            T value = list[k];
            list[k] = list[n];
            list[n] = value;
        }
    }
}

then in your code do something similar to

public class Program
{
    public static void Main(string[] args)
    {
        var list = new String[] {"Angie", "David", "Emily", "James"};

        var baseSeed = Environment.TickCount;
        for (int i = 0; i < 100; i++)
        {
            Random rnd = new Random(baseSeed + i / 10);


            var shuffled = list.ToList(); //This is so we don't mutate the original list with Shuffle
            shuffled.Shuffle(rnd);
            Console.WriteLine(String.Join(",", shuffled));
            Thread.Sleep(100);
        }
        Console.ReadLine();
    }
}

This would make shuffled have the same result 10 times in a row then it would change to a new order and keep that new order for 10 times in a row. Every time you run the program you will get a different result because it will use a new base Environment.TickCount to start it's offset from. If you just use new Random() it actually just does new Random(Environment.TickCount)

Community
  • 1
  • 1
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
2

Random class will always generate the same sequence with the same seed. So your code can be:

Random rnd = new Random(12345);  //a constant seed (time? userid? dayOfWeek? etc)
var shuffled = list.OrderBy(v => rnd.Next()).ToList();
Eser
  • 12,346
  • 1
  • 22
  • 32