0

So I'm trying to copy an array to a list in a randomized order, and it's "kind of" working, I'm getting a random order output except that it doesn't seem to be "completely" random, that is, there are certain sequences of numbers that seem to repeat many times they are specifically 1-9. Now they don't always appear in a sequence and the position of the sequence relative to the other numbers changes, but I am seeing them appear an abnormal amount of times for a "randomized list".

Here's my code:

class Program
{
    static void Main(string[] args)
    {
        int[] originalDeck = new int[52];

        for (int i = 1; i < originalDeck.Length+1; i++)
        {
           originalDeck[i-1] = i;
        }

        Random RNG = new Random();
        List<int> split1 = originalDeck.OrderBy(x => x < RNG.Next(52)).ToList();

        PrintList1(split1); 
        Console.ReadKey();
    }
    static void PrintList1(List<int> split)
    {
        foreach (int card in split)
        {
            Console.WriteLine(card);
        }
    }
}
Overly Excessive
  • 2,095
  • 16
  • 31
  • 2
    http://stackoverflow.com/questions/767999/random-number-generator-only-generating-one-random-number – DarkBee Feb 25 '14 at 11:41
  • 1
    You might want to lookup some methods for randomizing a list, e.g.: http://stackoverflow.com/questions/273313/randomize-a-listt-in-c-sharp – Paddy Feb 25 '14 at 11:41
  • Thats because `Random` works with timestamp to seed – DarkBee Feb 25 '14 at 11:42
  • Right, but I'm not reinitializing `Random`, it's calling `Next()` for each iteration of the loop, isn't that the right way to do it? – Overly Excessive Feb 25 '14 at 11:43
  • 1
    Try this: `List split1 = originalDeck.OrderBy(x => RNG.Next()).ToList()` – Matthew Watson Feb 25 '14 at 11:43
  • @OverlyExcessive your new Random is called with in 1ms from each other -> same seed every time – DarkBee Feb 25 '14 at 11:46
  • @DarkBee No it's not. Look at the code again! There's no creation of a `Random` object in a loop. – Matthew Watson Feb 25 '14 at 11:51
  • @MatthewWatson that seemed to work, but can you just explain how that statement works? Is it the same as writing `OrderBy(x => x == RNG.Next())` ? – Overly Excessive Feb 25 '14 at 11:52
  • @MatthewWatson True, read to fast :) – DarkBee Feb 25 '14 at 11:53
  • @OverlyExcessive No it's not the same. It works by randomising the order of elements by turning the values that `OrderBy` would use to order the array into random numbers, which results in a random ordering. – Matthew Watson Feb 25 '14 at 11:54
  • @MatthewWatson Yeah I just looked up the `OrderBy()` method and it's a lot more complex than I thought, I thought it worked like a Predicate but apparently it is more flexible than that.. – Overly Excessive Feb 25 '14 at 12:07

1 Answers1

0

What happens here is that you sort the array based on the probability of the number to be less than a random number between 0 and 51. The OrderBy function in your case returns a boolean value of either True or False, the internal implementation works such that the sorted array will have all the false in the beginning followed by all the true. You can see this by sorting a list of Boolean values. In your case, you pick a random number between 0 and 51 and then compare it to your number. The probability of the check x < RNG.Next(52) to return True is much higher for small numbers and False for large numbers. Therefore you are actually putting the larger numbers at the beginning of the array and the smaller numbers at the end of the array with some randomness.

About the seed issue. It is not related directly since the seed is determined once when you create the Random instance.

Buzzy
  • 2,905
  • 3
  • 22
  • 31