Inner workings or orderby Rnd.next

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jrmourning
    New Member
    • Jul 2012
    • 4

    Inner workings or orderby Rnd.next

    I am trying to understand the internal logic of how Orderby actually re-orders when using Orderby with random.Next.

    For Example in the following how is the random number actually used to do the reordering?

    Code:
    Dim deck = Enumerable.Range(1, 52)
    
    deck = deck.OrderBy(Function(n) rng.Next)
    I would appreciate an easy not too technical explanation.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    I have no idea what you are doing with the random number.
    The Enummerable.Ord erBy() method is used to sort the elements in the enumeration. Using a random number is kind of defeating the purpose because it would be randomizing instead of sorting. See the link for more details.



    -Frinny

    Comment

    • jrmourning
      New Member
      • Jul 2012
      • 4

      #3
      this was part of the code in a Dan Mabbutt article concerning random numbers. the entire code block was
      Code:
      Dim rng As New Random
      'Create the deck.
      Dim deck = Enumerable.Range(1, 52)
      'Shuffle the deck.
      deck = deck.OrderBy(Function(n) rng.Next)
      'Deal four hands of five cards.
      Dim hands(3) As List(Of Integer)
      For i = 0 To 3
          hands(i) = deck.Skip(i * 5).Take(5).ToList()
      Next
      I understand the effect of the code; however, when stepping through it, the OrderBy Rng.Next cycles through each of the members of deck. I am wondering about the mechanics of exactly how it is randomizing the members of deck. When it examines the first member is it selecting a subsequent member at random and replacing the member it is addressing? Is it then simply placing the replaced member at the position from which it took the replacement?

      I am interested in the details of the process that is being performed.

      Dan's article is at http://visualbasic.about.com/od/usin...ndintarray.htm

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Well, if your list was originally sorted (in order) and you sort it again based on a random number, then your sorted list will not be sorted any more.

        -Frinny

        Comment

        Working...