C# - Orderby with Random Number between 0 to 1

Shervan360 1,661 Reputation points
2021-12-07T19:00:49.667+00:00

Hello,

I wrote a method but I don't know how is it work!

random.NextDouble() method generate random number between 0 to 1. Now, how Orderby method use this number?
With which criteria Orderby work?

My main problem with the following code in the program:

list.OrderBy(x => random.NextDouble()); 

Thanks

using System;

namespace My
{
    public static class MyExtensions
    {
        static Random random = new Random();
        public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> list)
        {
            return list.OrderBy(x => random.NextDouble()); 
            // How OrderBy work with random number between 0 to 1?
        }
    }
    class Program
    {
        public static void Main()
        {
            var list = new[] { 1, 30, 23, 33, 5 };

            list.Shuffle();

            var list2 = new[] { 'a', 'c', 'b','d' };

            list2.Shuffle();

            foreach (var item in list.Shuffle())
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("/////");

            foreach (var item in list2.Shuffle())
            {
                Console.WriteLine(item);
            }
        }
    }
}
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 60,161 Reputation points
    2021-12-07T19:32:50.427+00:00

    OrderBy uses the returned value to order items in ascending order. Suppose you had this scenario.

    var numbers = new [] { 3, 5, 8, 10, 4, 2 };
    
    var ordered = numbers.OrderBy(x => x);
    

    In this case the numbers would be returned ordered by their value ascending (2, 3, 4, 5, 8, 10). That's how OrderBy works. It doesn't matter where the value comes from, it simply orders the items based upon value returned for each item.

    In your specific case you are returning a random number each time. Therefore the items will be randomly ordered when they are returned.

    var randomOrder = numbers.Shuffle();
    

    This might return 2, 8, 3, 5, 10, 4 or 3, 8, 5, 4, 10, 3. The order is completely determined by the randomness of the values generated. But do note that we generally assume consistent ordering so the order that the values are returned in can change each time the enumerator (that is returned by Shuffle) is called. In this example it is called one time.

    foreach (var result in numbers.Shuffle())
    {
    };
    

    But look at this case.

    var randomOrder = numbers.Shuffle();
    var results1 = randomOrder.ToArray();
    var results2 = randomOrder.ToArray();
    

    Here the returned enumerator is called multiple times and each time it orders the items again. Given that the ordering is random results1 will produce a different ordering then results2.

    Given your purpose there is nothing wrong with this Shuffle method. It is doing what you might expect if you were, say, shuffling a deck of cards.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Jim Vaughan 1 Reputation point
    2021-12-07T19:31:33.143+00:00

    It works because the random numbers can be compared to each other. If the second number is higher than the first, the first element is sorted in a lower position and so on.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.