Visual Basic (.NET) > 随机化列表框选择(唯一)
Visual Basic (.NET) > randomize listbox picks (unique)
我只是想制作随机化器程序。它将用于挑选锦标赛对(例如 f.e。欧洲冠军联赛四分之一决赛对)
GUI 屏幕:http://i.imgur.com/jqBMJjt.png
我遇到了麻烦..当我尝试制作选择器时,它运行良好 > 从列表框 1(左侧)到列表框 2(列表框 1 项的 50%)、列表框 3(列表框 1 项的 50%).. (左侧)但这些选择并不是独一无二的。正如您在图像上看到的那样,有一些重复项(第二个列表框 2x noob)。
我的部分代码:
Private Sub RandomiseListBox()
Dim count As Integer = CarbonFiberListBox1.Items.Count
Dim countt As Integer
'countt = count / 2
Dim item As String
Dim itemz As New List(Of String)()
Dim repeat As New List(Of String)()
Dim aa, bb As Integer
If Not count = 0 And ((count Mod 2) = 0) Then
CarbonFiberListBox2.Items.Clear()
CarbonFiberListBox3.Items.Clear()
For index As Integer = 0 To countt - 1 Step 1
item = Me.CarbonFiberListBox1.Items(Me.randomiser.Next(index, count))
itemz.Add(item)
'Me.CarbonFiberListBox1.Items.Remove(item)
'Me.CarbonFiberListBox1.Items.Insert(index, item)
Me.CarbonFiberListBox2.Items.Insert(index, item)
Next index
For index As Integer = 0 To countt - 1 Step 1
For aa = 0 To bb = 999
item = Me.CarbonFiberListBox1.Items(Me.randomiser.Next(index, count))
If Not (itemz.Contains(item)) And Not (repeat.Contains(item)) Then
repeat.Add(item)
'Me.CarbonFiberListBox1.Items.Remove(item)
'Me.CarbonFiberListBox1.Items.Insert(index, item)
Me.CarbonFiberListBox3.Items.Insert(index, item)
End If
Next
Next index
'For index As Integer = 0 To countt - 1 Step 1
'Next index
ElseIf count > 0 Then
'CarbonFiberButton4.Text = "ODD PARTICIPANTS!"
Else
End If
End Sub
我能得到帮助吗?我觉得很简单。
也许这个小例子会给你一个想法。它通过从一个列表框中以随机顺序获取列表来工作。请注意列表是如何随着项目添加到其他列表框而耗尽的,从而停止重复。
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'some sample data
ListBox1.DataSource = {"one", "two", "three", "four", "five", "six", "seven"}
DoRandom()
End Sub
Private Shared prng As New Random
Private Sub DoRandom()
ListBox2.Items.Clear()
ListBox3.Items.Clear()
Dim l As New List(Of String)
'random order of items in ListBox1
l.AddRange(ListBox1.Items.Cast(Of String).OrderBy(Function(s) prng.Next))
'add half to lb2
For x As Integer = 0 To l.Count \ 2
ListBox2.Items.Add(l(0))
l.RemoveAt(0)
Next
'remainder to lb3
For x As Integer = 0 To l.Count - 1
ListBox3.Items.Add(l(0))
l.RemoveAt(0)
Next
End Sub
我只是想制作随机化器程序。它将用于挑选锦标赛对(例如 f.e。欧洲冠军联赛四分之一决赛对)
GUI 屏幕:http://i.imgur.com/jqBMJjt.png
我遇到了麻烦..当我尝试制作选择器时,它运行良好 > 从列表框 1(左侧)到列表框 2(列表框 1 项的 50%)、列表框 3(列表框 1 项的 50%).. (左侧)但这些选择并不是独一无二的。正如您在图像上看到的那样,有一些重复项(第二个列表框 2x noob)。
我的部分代码:
Private Sub RandomiseListBox()
Dim count As Integer = CarbonFiberListBox1.Items.Count
Dim countt As Integer
'countt = count / 2
Dim item As String
Dim itemz As New List(Of String)()
Dim repeat As New List(Of String)()
Dim aa, bb As Integer
If Not count = 0 And ((count Mod 2) = 0) Then
CarbonFiberListBox2.Items.Clear()
CarbonFiberListBox3.Items.Clear()
For index As Integer = 0 To countt - 1 Step 1
item = Me.CarbonFiberListBox1.Items(Me.randomiser.Next(index, count))
itemz.Add(item)
'Me.CarbonFiberListBox1.Items.Remove(item)
'Me.CarbonFiberListBox1.Items.Insert(index, item)
Me.CarbonFiberListBox2.Items.Insert(index, item)
Next index
For index As Integer = 0 To countt - 1 Step 1
For aa = 0 To bb = 999
item = Me.CarbonFiberListBox1.Items(Me.randomiser.Next(index, count))
If Not (itemz.Contains(item)) And Not (repeat.Contains(item)) Then
repeat.Add(item)
'Me.CarbonFiberListBox1.Items.Remove(item)
'Me.CarbonFiberListBox1.Items.Insert(index, item)
Me.CarbonFiberListBox3.Items.Insert(index, item)
End If
Next
Next index
'For index As Integer = 0 To countt - 1 Step 1
'Next index
ElseIf count > 0 Then
'CarbonFiberButton4.Text = "ODD PARTICIPANTS!"
Else
End If
End Sub
我能得到帮助吗?我觉得很简单。
也许这个小例子会给你一个想法。它通过从一个列表框中以随机顺序获取列表来工作。请注意列表是如何随着项目添加到其他列表框而耗尽的,从而停止重复。
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'some sample data
ListBox1.DataSource = {"one", "two", "three", "four", "five", "six", "seven"}
DoRandom()
End Sub
Private Shared prng As New Random
Private Sub DoRandom()
ListBox2.Items.Clear()
ListBox3.Items.Clear()
Dim l As New List(Of String)
'random order of items in ListBox1
l.AddRange(ListBox1.Items.Cast(Of String).OrderBy(Function(s) prng.Next))
'add half to lb2
For x As Integer = 0 To l.Count \ 2
ListBox2.Items.Add(l(0))
l.RemoveAt(0)
Next
'remainder to lb3
For x As Integer = 0 To l.Count - 1
ListBox3.Items.Add(l(0))
l.RemoveAt(0)
Next
End Sub