Vb.net 随机名称生成器

Vb.net 随机名称生成器,vb.net,random,Vb.net,Random,我正在尝试创建一个随机名称生成器,它实际上选择了我列出的其中一个名称,并将其放在Textbox1中,问题是它不是随机选择,它只是始终遵循相同的顺序 第一次点击:佩德罗·里贝罗 第二次点击:Paulo Lins 第三次点击:罗德里戈·马丁斯 等等 表格 名称代码 Dim NameKey As Integer NameKey = Int(Rnd() * 10) Select Case NameKey Case 0 TextBox1.Text = "Will" Cas

我正在尝试创建一个随机名称生成器,它实际上选择了我列出的其中一个名称,并将其放在Textbox1中,问题是它不是随机选择,它只是始终遵循相同的顺序

第一次点击:佩德罗·里贝罗

第二次点击:Paulo Lins

第三次点击:罗德里戈·马丁斯

等等

表格

名称代码

Dim NameKey As Integer
NameKey = Int(Rnd() * 10)
Select Case NameKey
    Case 0
        TextBox1.Text = "Will"
    Case 1
        TextBox1.Text = "Bernardo"
    Case 2
        TextBox1.Text = "Manoel"
Etc...
Dim LastNameKey As Integer
LastNameKey = Int(Rnd() * 10)
Select Case LastNameKey
    Case 0
        TextBox2.Text = "Monteiro"
    Case 1
        TextBox2.Text = "Ferraz"
    Case 2
        TextBox2.Text = "Lins"
Etc...
姓氏代码

Dim NameKey As Integer
NameKey = Int(Rnd() * 10)
Select Case NameKey
    Case 0
        TextBox1.Text = "Will"
    Case 1
        TextBox1.Text = "Bernardo"
    Case 2
        TextBox1.Text = "Manoel"
Etc...
Dim LastNameKey As Integer
LastNameKey = Int(Rnd() * 10)
Select Case LastNameKey
    Case 0
        TextBox2.Text = "Monteiro"
    Case 1
        TextBox2.Text = "Ferraz"
    Case 2
        TextBox2.Text = "Lins"
Etc...

以下是实现您想要的众多方法之一。 我创建了一个简单的结构来正确处理信息

'The structure will ensure coherent data
Public Structure Person
    Public firstName As String
    Public lastName As String
    Public Sub New(fName As String, lName As String)
        firstName = fName
        lastName = lName
    End Sub
End Structure

Public Sub AnySub()

    'You should use lists or dictionaries when you manipulate collections
    Dim ListNames As New List(Of Person)
    ListNames.Add(New Person("Shin", "Juku"))
    ListNames.Add(New Person("Yaki", "Tori"))
    ListNames.Add(New Person("Fuji", "San"))

    'This will initialize and randomize the counter
    'Since the seed is time dependent, the result will be (almost) different each time you call it
    Dim rnd As New Random(Now.Millisecond)

    For i As Integer = 0 To 5
        'index will change at each iteration
        'I specify ListNames.Count as a maximum so I can use it as a counter of ListNames
        Dim index As Integer = rnd.Next(ListNames.Count)

        'retrieve a random person in the list and displaying its information
        Dim current As Person = ListNames(index)
        MsgBox(current.firstName & " " & current.lastName)
    Next

End Sub

以下是实现您想要的众多方法之一。 我创建了一个简单的结构来正确处理信息

'The structure will ensure coherent data
Public Structure Person
    Public firstName As String
    Public lastName As String
    Public Sub New(fName As String, lName As String)
        firstName = fName
        lastName = lName
    End Sub
End Structure

Public Sub AnySub()

    'You should use lists or dictionaries when you manipulate collections
    Dim ListNames As New List(Of Person)
    ListNames.Add(New Person("Shin", "Juku"))
    ListNames.Add(New Person("Yaki", "Tori"))
    ListNames.Add(New Person("Fuji", "San"))

    'This will initialize and randomize the counter
    'Since the seed is time dependent, the result will be (almost) different each time you call it
    Dim rnd As New Random(Now.Millisecond)

    For i As Integer = 0 To 5
        'index will change at each iteration
        'I specify ListNames.Count as a maximum so I can use it as a counter of ListNames
        Dim index As Integer = rnd.Next(ListNames.Count)

        'retrieve a random person in the list and displaying its information
        Dim current As Person = ListNames(index)
        MsgBox(current.firstName & " " & current.lastName)
    Next

End Sub

你会发现它是相关的。NET
Random
类比传统的
Rnd
函数更容易使用。你是不是先随机化了?此外,只需一点linq,您就可以从2个集合中创建一个随机名称列表,代码要少得多。如何替换?您会发现这是相关的。NET
random
类比传统的
Rnd
函数更易于使用。你是不是先随机化了?另外,只需一点linq,您就可以用更少的代码从2个集合中创建一个随机名称列表。如何替换?