简体   繁体   English

VB.NET 中的随机随机文本框行

[英]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输出:经过shuffle()随机

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.它们现在是一组重载的Extension方法,适合包含在通用工具库中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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