简体   繁体   中英

Random Shuffle Textbox Lines in VB.NET

How can I randomize (shuffle) textbox lines? these lines in the textbox, I have to arrange them again, randomly. the numbers will remain the same, the line will be changed.

1,2
3,5
7,9
12,15
21,24
31,36
41,49
51,54

Output: after shuffle () random

3,5
1,2
7,9
12,15
31,36
21,24
51,54
41,49

Code:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    TextBox1.Text(RandomizeList)
End Sub

' Randomize the list.
Public Sub RandomizeList(items() As Integer)
    Dim min_item As Integer
    Dim max_item As Integer
    Dim i As Integer
    Dim j As Integer
    Dim tmp_value As Integer

    min_item = LBound(items)
    max_item = UBound(items)

    For i = min_item To max_item - 1
        ' Randomly assign item number i.
        j = Int((max_item - i + 1) * Rnd() + i)
        tmp_value = items(i)
        items(i) = items(j)
        items(j) = tmp_value
    Next i
End Sub

Expected Output:

to shuffle the lines of the textbox.

The simplest way to shuffle a list is this:

Dim list = New List(Of String) From {"1,2", "3,5", "7,9", "12,15", "21,24", "31,36", "41,49", "51,54"}
Dim rnd As New Random()
Dim shuffled = list.OrderBy(Function (x) rnd.Next()).ToList()

This ought to do it:

<TestMethod>
Public Sub RandomizeList()
  Dim oControl As List(Of Integer)
  Dim oOldList As List(Of String)
  Dim oNewList As List(Of String)
  Dim iRandom As Integer
  Dim oRandom As Random

  oControl = New List(Of Integer)
  oOldList = New List(Of String) From {"1,2", "3,5", "7,9", "12,15", "21,24", "31,36", "41,49", "51,54"}
  oNewList = New List(Of String)
  oRandom = New Random

  oOldList.ForEach(Sub(Item)
                     Do
                       iRandom = oRandom.Next(oOldList.Count)
                     Loop While oControl.Contains(iRandom)

                     oControl.Add(iRandom)
                     oNewList.Add(oOldList(iRandom))
                   End Sub)

  Debug.WriteLine("")
  Debug.WriteLine("Old List")
  Debug.WriteLine("----------")
  oOldList.ForEach(Sub(Item) Debug.WriteLine(Item))

  Debug.WriteLine("")
  Debug.WriteLine("New List")
  Debug.WriteLine("----------")
  oNewList.ForEach(Sub(Item) Debug.WriteLine(Item))
End Sub

--UPDATE--

Here's another way to accomplish the task, somewhat cleaner:

  <Extension>
  Public Function Randomize(Of T)(Instance As IEnumerable(Of T)) As IEnumerable(Of T)
    Return Instance.Randomize(New Random())
  End Function

  <Extension>
  Public Iterator Function Randomize(Of T)(Instance As IEnumerable(Of T), Rng As Random) As IEnumerable(Of T)
    Dim oBuffer As List(Of T)
    Dim iRandom As Integer
    Dim iIndex As Integer

    Instance.ThrowIfNothing(NameOf(Instance))
    Rng.ThrowIfNothing(NameOf(Rng))

    oBuffer = Instance.ToList

    For iIndex = 0 To oBuffer.Count - 1
      iRandom = Rng.Next(iIndex, oBuffer.Count)
      Yield oBuffer(iRandom)
      oBuffer(iRandom) = oBuffer(iIndex)
    Next
  End Function

They're now an overloaded set of Extension methods, suitable for inclusion in a common tools library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM