Randomly Generate First and Last Names

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 2597 Views - Last Post: 14 May 2012 - 02:37 PM Rate Topic: -----

#1 xilith117  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 68
  • Joined: 13-August 11

Randomly Generate First and Last Names

Posted 09 May 2012 - 07:23 PM

I do VB as a hobby. I got an idea for a good practice program but I don't quite know how to do this certain section of it. I want it to take a group of first names and randomly pair one with a last name randomly selected from a separate group. something like this:
Group 1-
  • Bob
  • Joe
  • Brian
  • Jill
  • Jorge


Group 2-
  • Sanders
  • Rientz
  • Lopez
  • Gump
  • Staudmann

then output something like "Brian Staudmann" or "Jorge Gump"

Except I want a significantly larger selection of first and last names in each group. Would it be best to save them in an array? A String? Parse from textfiles? and how? What should my code look like? Thanks a ton. :)

Is This A Good Question/Topic? 0
  • +

Replies To: Randomly Generate First and Last Names

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9058
  • View blog
  • Posts: 34,007
  • Joined: 12-June 08

Re: Randomly Generate First and Last Names

Posted 09 May 2012 - 07:27 PM

You could use a dataset.. a table of first names.. a table of last.. read in from a text file.. or you could just read in from a text file a list of first names and a list of second name strings and store them in two list(of string) collections... either way isn't bad.. the same setup of reading in from a text file and adding to a collection (of some sort).
Was This Post Helpful? 1
  • +
  • -

#3 xilith117  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 68
  • Joined: 13-August 11

Re: Randomly Generate First and Last Names

Posted 09 May 2012 - 07:31 PM

ok...lets try datasets...great..how do i do that? haha. im really sorry. :)
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9058
  • View blog
  • Posts: 34,007
  • Joined: 12-June 08

Re: Randomly Generate First and Last Names

Posted 09 May 2012 - 07:53 PM

Right click - add to project.. dataset.

In the data set.. right click.. add.. datatable.. do that twice.. add a column a piece.
Was This Post Helpful? 0
  • +
  • -

#5 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 583
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Randomly Generate First and Last Names

Posted 09 May 2012 - 07:55 PM

Do you know how to use DataSets? If not, look here:
http://msdn.microsof...ta.dataset.aspx
Was This Post Helpful? 1
  • +
  • -

#6 polens  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 155
  • Joined: 09-November 09

Re: Randomly Generate First and Last Names

Posted 09 May 2012 - 08:07 PM

you can put it in a list or an array then used random method..
i created 500k records for my personal test data with this method and save it to the database...took only 5-10 mins...
Was This Post Helpful? 0
  • +
  • -

#7 Nerfpl  Icon User is offline

  • D.I.C Head

Reputation: 43
  • View blog
  • Posts: 165
  • Joined: 15-January 12

Re: Randomly Generate First and Last Names

Posted 09 May 2012 - 08:12 PM

I would use List<String> for this. DataSet is overkill in my opinion.

// pseudo syntax
name = names[random.Next(0,names.count-1)]
lastname = lastnames[random.Next(0,lastnames.count-1)]


Was This Post Helpful? 1
  • +
  • -

#8 xilith117  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 68
  • Joined: 13-August 11

Re: Randomly Generate First and Last Names

Posted 09 May 2012 - 08:22 PM

Dataset does seem a bit exorbitant. as soon as i make heads-or-tails of that psuedocode i'll try this method. haha. Sorry again guys, I hate dumb people online XD
Was This Post Helpful? 0
  • +
  • -

#9 xilith117  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 68
  • Joined: 13-August 11

Re: Randomly Generate First and Last Names

Posted 11 May 2012 - 03:32 PM

Ummmm...I think this is a step in the right direction?

Dim values As String
        values = "bob, moe, steve, greg, adam"
        Dim sites As String() = Nothing
        sites = values.Split(",")

Was This Post Helpful? 0
  • +
  • -

#10 polens  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 155
  • Joined: 09-November 09

Re: Randomly Generate First and Last Names

Posted 11 May 2012 - 08:00 PM

put the names in an array or generic List.
values As String() = {"a", "b", "c"}
        Dim x As Integer
        Dim ran As New Random
        x = ran.Next(0, values.Length - 1)


something like that do it for the last name
Was This Post Helpful? 1
  • +
  • -

#11 xilith117  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 68
  • Joined: 13-August 11

Re: Randomly Generate First and Last Names

Posted 11 May 2012 - 09:52 PM

Thank you for that. I'm pretty sure I understand now. I'll try it as soon as I can. Which may be a Day or two...
Was This Post Helpful? 0
  • +
  • -

#12 polens  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 155
  • Joined: 09-November 09

Re: Randomly Generate First and Last Names

Posted 11 May 2012 - 11:31 PM

how to get the values will be like
values.getvalues(x).tostring

Was This Post Helpful? 1
  • +
  • -

#13 xilith117  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 68
  • Joined: 13-August 11

Re: Randomly Generate First and Last Names

Posted 12 May 2012 - 06:09 AM

Ok, I didn't follow the examples PERFECTLY but I finally understood the basic idea. Here's my code so far:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim first(19) As String
        Dim last(19) As String
        Dim indcnctor As Integer = 0
        Dim cnctors(4) As String
        Dim rndVal As New Random
        Dim whole As String = ""
        Dim ind As Integer
        Dim foriterations As Integer
        Dim line As String = ""
        ComboBox2.Items.Clear()


        If RadioButton3.Checked = True Then 'radio3 = english name. I will have more radio buttons 
            last(0) = "Walker"              'for diffent types of names before i'm done.
            last(1) = "Stanton"
            last(2) = "Ausman"
            last(3) = "Nedry"
            last(4) = "Candric"
            last(5) = "Longman"
            last(6) = "Chang"
            last(7) = "Vickson"
            last(8) = "Derpston"
            last(9) = "Smith"
            last(10) = "Cooper"
            last(11) = "Peterson"
            last(12) = "Vanguard"
            last(13) = "LaPierre"
            last(14) = "Chase"
            last(15) = "Cromwell"
            last(16) = "Livingston"
            last(17) = "Ericson"
            last(18) = "Greyson"
            last(19) = "Felton"
        End If

        If RadioButton1.Checked = True And RadioButton3.Checked = True Then 'radio1 = male name.
            first(0) = "George"
            first(1) = "Wallace"
            first(2) = "William"
            first(3) = "Scott"
            first(4) = "Harold"
            first(5) = "Steven"
            first(6) = "John"
            first(7) = "Adam"
            first(8) = "Bartholomew"
            first(9) = "Sam"
            first(10) = "Alan"
            first(11) = "Sean"
            first(12) = "Wayne"
            first(13) = "Joseph"
            first(14) = "Carl"
            first(15) = "Andrew"
            first(16) = "Michael"
            first(17) = "Victor"
            first(18) = "Mark"
            first(19) = "Jeff"
        ElseIf RadioButton2.Checked = True And RadioButton3.Checked = True Then 'radio2 = female name.
            first(0) = "Abby"
            first(1) = "Mary"
            first(2) = "Alexus"
            first(3) = "Carol"
            first(4) = "Cayla"
            first(5) = "Lisbeth"
            first(6) = "Elizibeth"
            first(7) = "Marilyn"
            first(8) = "Michelle"
            first(9) = "Scarlett"
            first(10) = "Samantha"
            first(11) = "Whitney"
            first(12) = "Brittany"
            first(13) = "Juanita"
            first(14) = "Vicki"
            first(15) = "Julie"
            first(16) = "Ashley"
            first(17) = "Jen"
            first(18) = "Sally"
            first(19) = "Veronica"
        End If


        

        ' Generate one name .
        If CheckBox1.Checked = True Then
            'If checked, it only puts one name in a 
            'textbox.Otherwise it makes 10 and puts
            'them in a combobox.
            ind = rndVal.Next(0, first.Length)
            line &= first(ind) & " "

            ind = rndVal.Next(0, last.Length)
            line &= last(ind) & " "



            ' Add line to whole
            whole &= line


            ' Move to next cnctor
            indcnctor += 1

            ' If the cnctor is at the end of the array, reset it.
            If indcnctor >= cnctors.Length Then
                indcnctor = 0
            End If

            ' Now show whole in textbox
            TextBox1.Text = whole
        Else
            'generate 10 names
            For foriterations = 1 To 10

                ind = rndVal.Next(0, first.Length)
                line &= first(ind) & " "

                ind = rndVal.Next(0, last.Length)
                line &= last(ind) & " "

                'put names in combobox
                ComboBox2.Items.Add(line)

                line = Nothing
                ProgressBar1.Value = ProgressBar1.Value + 10
            Next

            ' Move to next cnctor
            indcnctor += 1

            ' If the cnctor is at the end of the array, reset it.
            If indcnctor >= cnctors.Length Then
                indcnctor = 0
            End If
            gress.Text = "Done." 'ignore this...
            percent.Text = "100" + "%"

        End If
    End Sub

It does work...i'm not sure how efficiently...but it DOES WORK. I apologize because I realize its a tad messy but I need to run out the door right now so I don't have time to clean it.
Was This Post Helpful? 0
  • +
  • -

#14 Nerfpl  Icon User is offline

  • D.I.C Head

Reputation: 43
  • View blog
  • Posts: 165
  • Joined: 15-January 12

Re: Randomly Generate First and Last Names

Posted 12 May 2012 - 10:42 AM

It's all fine tho it's pointless to fill array each time with same values.

Inside your class, outside of any method create those arrays and fill them once. Probably at initialization if they are static.
Was This Post Helpful? 0
  • +
  • -

#15 pradeep1210  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 56
  • Joined: 08-May 12

Re: Randomly Generate First and Last Names

Posted 13 May 2012 - 01:49 AM

If you fill each array item individually you would spend half your life just creating the array for a big list of first and last names. :eh: :no:

Instead go by this algorithm:
1. Create a file (say FirstNames.txt) and add your First Names in that file.
2. Create a file (say LastNames.txt) and add your Last Names in that file.
3. Read the FirstNames.txt file into an array.
4. Read the LastNames.txt file into another array.
5. Generate your names based on these two arrays.
6. Display the generated names whatever way you want to.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2