none
Explain about piece of code in C# - Random Numbers RRS feed

  • Question

  • Hello,

    I would generate a sequence between 1 and 49, shuffle them and pick 6 number out of the sequence.

    But could you please more explain about 

    OrderBy(x => rnd.Next())

    in

    var rnd = new Random();
    var randomNumbers = Enumerable.Range(1,49).OrderBy(x => rnd.Next()).Take(20).ToList();

    I'm confused how this code works.  OrderBy(x => rnd.Next())

    Thanks

    Friday, January 31, 2020 10:28 AM

Answers

  • If you have a list with 49 numbers and call OrderBy on that list, the delegate inside the OrderBy will execute 49 times and provide 49 values which define the order that you want to establish. Ordinarily, these values would come out of a field of the elements in the list you are ordering. But OrderBy doesn't care if the values come from somewhere else; it will order based on these values regardless. So, on each of the 49 calls you give back to OrderBy a different random number. It will then order based on these random numbers, resulting in the initial list being returned in a random order.
    • Proposed as answer by Belarmino Vicenzo Friday, January 31, 2020 12:10 PM
    • Marked as answer by Arash_89 Sunday, March 8, 2020 4:05 PM
    Friday, January 31, 2020 11:58 AM
    Moderator
  • Hi Arash_89,
    Alberto Poblacion has explained in detail. And Enumerable.OrderBy method is used to sort the elements of a sequence in ascending order.
    I find some related documents and hope these are useful for you.
    [Is using Random and OrderBy a good shuffle algorithm?]
    [Sort list in random number order]
    [Random numbers]

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, February 3, 2020 6:15 AM

All replies

  • If you have a list with 49 numbers and call OrderBy on that list, the delegate inside the OrderBy will execute 49 times and provide 49 values which define the order that you want to establish. Ordinarily, these values would come out of a field of the elements in the list you are ordering. But OrderBy doesn't care if the values come from somewhere else; it will order based on these values regardless. So, on each of the 49 calls you give back to OrderBy a different random number. It will then order based on these random numbers, resulting in the initial list being returned in a random order.
    • Proposed as answer by Belarmino Vicenzo Friday, January 31, 2020 12:10 PM
    • Marked as answer by Arash_89 Sunday, March 8, 2020 4:05 PM
    Friday, January 31, 2020 11:58 AM
    Moderator
  • Is that guaranteed to work?  That is, if there are 49 items in a list, does the system guarantee that OrderBy will be called exactly 49 times?  You can imagine, for example, a system that calls OrderBy twice for every comparison.  In that case, the results would be indeterminate.

    Of course, for shuffling purposes, I suppose that's just fine.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Sunday, February 2, 2020 5:07 AM
  • The value that we are passing to the OrderBy is an IEnumerable. It only provides a forward-only read, you can't access an arbitrary position in the IEnumerable. You can Reset it and start back from the beginning, but I doubt that the OrderBy is doing this, because it would be slow. So, in al likelihood, the internal implementation of OrderBy saves whatever information it needs on a single pass through the IEnumerable, and then sorts based on the values it saved.

    This is only a hypothesis; I don't know what the actual implementation of OrderBy is. In theory, it could be accessing each element more than once. If this happened, it would be generating random numbers more than 49 times. For the purpose of shuffling it doesn't matter, you still get a random order. But if you were doing a "real" sorting then you would need to be careful to always return the same value for each element that you sort.

    Sunday, February 2, 2020 8:46 AM
    Moderator
  • Hi Arash_89,
    Alberto Poblacion has explained in detail. And Enumerable.OrderBy method is used to sort the elements of a sequence in ascending order.
    I find some related documents and hope these are useful for you.
    [Is using Random and OrderBy a good shuffle algorithm?]
    [Sort list in random number order]
    [Random numbers]

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, February 3, 2020 6:15 AM
  • Hi Arash_89,
    Has your problem been solved? If it is resolved, we suggest that you mark it as the answer. So it can help other people who have the same problem find a solution quickly.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 6, 2020 8:14 AM