随机索引生成器

时间:2013-12-19 15:30:30

标签: vb.net visual-studio-2012 random

好的,所以我正在尝试制作一个程序来帮助我保持在Visual Basic技能之上,直到下学期。

所以我决定创建一个随机手机号码生成器,我想出了一个问题每当我选择“Bethany”作为我的城市时,无论我运行多少次,它都会拉“342”,而不是随机指数。以下是我的代码,也许你可以帮助我。

'======================================================

'Adam W. Schneider

'Random Cellphone number Generator

'VB 1 Refresh

'======================================================



Public Class vbform1

    'Clears the list box.

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles btnClear.Click

        'Clear the list Boxes

        lstPhoneNumbers.Items.Clear()
        lstBoxCity.Items.Clear()

    End Sub

    'Closes the Form.

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles btnExit.Click

        Me.Close()

    End Sub

    Private Sub btnExecute_Click(sender As Object, e As EventArgs) Handles btnExecute.Click

        'Determines the area code selected

        If lstAreaCodes.SelectedItem.ToString = "513" Then

            'Clears the box of previous results before displaying new results

            lstBoxCity.Items.Clear()

            'Writes the cities available for area code 513

            lstBoxCity.Items.Add("Cincinnati")
            lstBoxCity.Items.Add("Middletown")
            lstBoxCity.Items.Add("Lebanon (Warren)")
            lstBoxCity.Items.Add("South Lebanon")
            lstBoxCity.Items.Add("Mason")
            lstBoxCity.Items.Add("Little Miami")
            lstBoxCity.Items.Add("Oxford")
            lstBoxCity.Items.Add("Bethel")
            lstBoxCity.Items.Add("Harrison")
            lstBoxCity.Items.Add("Clermont")
            lstBoxCity.Items.Add("Bethany")
            lstBoxCity.Items.Add("Butlerville")
            lstBoxCity.Items.Add("Fayetteville")
            lstBoxCity.Items.Add("Felicity")
            lstBoxCity.Items.Add("Monroe")
            lstBoxCity.Items.Add("Morning Sun")
            lstBoxCity.Items.Add("Morrow")
            lstBoxCity.Items.Add("Newtonsville")
            lstBoxCity.Items.Add("Reily")
            lstBoxCity.Items.Add("Seven Mile")
            lstBoxCity.Items.Add("Shandon")
            lstBoxCity.Items.Add("Trenton")
            lstBoxCity.Items.Add("Waynesville")
            lstBoxCity.Items.Add("Williamsburg")

            'Determines the area code selected

        ElseIf lstAreaCodes.SelectedItem.ToString = "937" Then

            'Clears the box of previous results before displaying new results

            lstBoxCity.Items.Clear()

            'Writes the cities available for area code 937

            lstBoxCity.Items.Add("Aberdeen")
            lstBoxCity.Items.Add("Anna")
            lstBoxCity.Items.Add("Ansonia")
            lstBoxCity.Items.Add("Arcanum")
            lstBoxCity.Items.Add("Beavercreek")
            lstBoxCity.Items.Add("Belfast")
            lstBoxCity.Items.Add("Bellbrook")
            lstBoxCity.Items.Add("Belle Center")
            lstBoxCity.Items.Add("Bellefontaine")
            lstBoxCity.Items.Add("Blanchester")
            lstBoxCity.Items.Add("Botkins")
            lstBoxCity.Items.Add("Bowersville")
            lstBoxCity.Items.Add("Bradford")
            lstBoxCity.Items.Add("Brookville")
            lstBoxCity.Items.Add("Byhalia")
            lstBoxCity.Items.Add("Camden")
            lstBoxCity.Items.Add("Catawba")
            lstBoxCity.Items.Add("Cedarville")
            lstBoxCity.Items.Add("Centerville (Mongomery)")
            lstBoxCity.Items.Add("Christiansburg")
            lstBoxCity.Items.Add("Clarksville")
            lstBoxCity.Items.Add("Covington")
            lstBoxCity.Items.Add("Danville (Highland)")
            lstBoxCity.Items.Add("Dayton")
            lstBoxCity.Items.Add("De Graff")
            lstBoxCity.Items.Add("Decatur")
            lstBoxCity.Items.Add("Donnelsville")
            lstBoxCity.Items.Add("East Liberty")
            lstBoxCity.Items.Add("Eaton")
            lstBoxCity.Items.Add("Eldorado")
            lstBoxCity.Items.Add("Englewood")
            lstBoxCity.Items.Add("Enon")
            lstBoxCity.Items.Add("Fairborn")
            lstBoxCity.Items.Add("Famersville")
            lstBoxCity.Items.Add("Fletcher-Lena")
            lstBoxCity.Items.Add("Fort Loramie")
            lstBoxCity.Items.Add("Franklin")
            lstBoxCity.Items.Add("Georgetown")
            lstBoxCity.Items.Add("Germantown")
            lstBoxCity.Items.Add("Gettysburg")
            lstBoxCity.Items.Add("Gratis")
            lstBoxCity.Items.Add("Greenfield")
            lstBoxCity.Items.Add("Greenville")
            lstBoxCity.Items.Add("Hamersville")
            lstBoxCity.Items.Add("Higginsport")
            lstBoxCity.Items.Add("Hillsboro")
            lstBoxCity.Items.Add("Hollansburg")
            lstBoxCity.Items.Add("Huntsville")
            lstBoxCity.Items.Add("Jackson Center")
            lstBoxCity.Items.Add("Jamestown")

            'Determines the area code selected

        ElseIf lstAreaCodes.SelectedItem.ToString = "859" Then

            'Clears the box of previous results before displaying new results

            lstBoxCity.Items.Clear()

            'Writes the cities available for area code 859

            lstBoxCity.Items.Add("Alexandria")
            lstBoxCity.Items.Add("Berea")
            lstBoxCity.Items.Add("Boone")
            lstBoxCity.Items.Add("Bryantsville")
            lstBoxCity.Items.Add("Burgin")
            lstBoxCity.Items.Add("Butler")
            lstBoxCity.Items.Add("Carlisle")
            lstBoxCity.Items.Add("Cornishville")
            lstBoxCity.Items.Add("Convington")
            lstBoxCity.Items.Add("Cythiana")
            lstBoxCity.Items.Add("Danville")
            lstBoxCity.Items.Add("Falmouth")
            lstBoxCity.Items.Add("Ford")
            lstBoxCity.Items.Add("Glencole")
            lstBoxCity.Items.Add("Harrodsburg")
            lstBoxCity.Items.Add("Independence")
            lstBoxCity.Items.Add("Junction City")
            lstBoxCity.Items.Add("Kirksville")
            lstBoxCity.Items.Add("Lancaster")
            lstBoxCity.Items.Add("Lexington")
            lstBoxCity.Items.Add("Little Rock")
            lstBoxCity.Items.Add("Mackville")
            lstBoxCity.Items.Add("Midway")
            lstBoxCity.Items.Add("Millersburg")
            lstBoxCity.Items.Add("Mooresville")
            lstBoxCity.Items.Add("North Middletown")
            lstBoxCity.Items.Add("Paint Lick")
            lstBoxCity.Items.Add("Paris")
            lstBoxCity.Items.Add("Perryville")
            lstBoxCity.Items.Add("Richmond")
            lstBoxCity.Items.Add("Salvisa")
            lstBoxCity.Items.Add("Springfield")
            lstBoxCity.Items.Add("Versailles")
            lstBoxCity.Items.Add("Waco")
            lstBoxCity.Items.Add("Walton")
            lstBoxCity.Items.Add("Warsaw")
            lstBoxCity.Items.Add("Williamstown")
            lstBoxCity.Items.Add("Willisburg")
            lstBoxCity.Items.Add("Wilmore")
            lstBoxCity.Items.Add("Winchester")

            'Determines the area code selected

        ElseIf lstAreaCodes.SelectedItem.ToString = "812" Then

            'Clears the box of previous results before displaying new results

            lstBoxCity.Items.Clear()

            'Writes the cities available for area code 812

            lstBoxCity.Items.Add("Alfordsville-Glendale")
            lstBoxCity.Items.Add("Aurora")
            lstBoxCity.Items.Add("Austin")
            lstBoxCity.Items.Add("Brandon")
            lstBoxCity.Items.Add("Batesville")
            lstBoxCity.Items.Add("Bedford")
            lstBoxCity.Items.Add("Bicknell")
            lstBoxCity.Items.Add("Birdseye")
            lstBoxCity.Items.Add("Bloomfield")
            lstBoxCity.Items.Add("Boonville")
            lstBoxCity.Items.Add("Brazil")
            lstBoxCity.Items.Add("Brownstown")
            lstBoxCity.Items.Add("Bruceville")
            lstBoxCity.Items.Add("Butlerville")
            lstBoxCity.Items.Add("Campbellsburg")
            lstBoxCity.Items.Add("Canaan")
            lstBoxCity.Items.Add("Center Point")
            lstBoxCity.Items.Add("Central")
            lstBoxCity.Items.Add("Chandler")
            lstBoxCity.Items.Add("Charlestown")
            lstBoxCity.Items.Add("Chrisney")
            lstBoxCity.Items.Add("Clay City")
            lstBoxCity.Items.Add("Clearspring")
            lstBoxCity.Items.Add("Columbus")
            lstBoxCity.Items.Add("Cory")
            lstBoxCity.Items.Add("Corydon")
            lstBoxCity.Items.Add("Crandall")
            lstBoxCity.Items.Add("Crane")
            lstBoxCity.Items.Add("Cross Plains")
            lstBoxCity.Items.Add("Crothersville")
            lstBoxCity.Items.Add("Cynthiana")
            lstBoxCity.Items.Add("Dale")
            lstBoxCity.Items.Add("Decker")
            lstBoxCity.Items.Add("Dillsboro")
            lstBoxCity.Items.Add("Dubois")
            lstBoxCity.Items.Add("Dugger")
            lstBoxCity.Items.Add("East Enterprise")
            lstBoxCity.Items.Add("Edinburgh")
            lstBoxCity.Items.Add("Elberfeld")
            lstBoxCity.Items.Add("Elizabeth")
            lstBoxCity.Items.Add("Elizabethtown")
            lstBoxCity.Items.Add("Elletsville")
            lstBoxCity.Items.Add("Elnora")
            lstBoxCity.Items.Add("English")
            lstBoxCity.Items.Add("Evansville")
            lstBoxCity.Items.Add("Fairbanks")
            lstBoxCity.Items.Add("Farmersburg")
            lstBoxCity.Items.Add("Ferdinand")
            lstBoxCity.Items.Add("Flat Rock")

            'Determines the area code selected

        ElseIf lstAreaCodes.SelectedItem.ToString = "765" Then

            'Clears the box of previous results before displaying new results

            lstBoxCity.Items.Clear()

            'Writes the cities available for area code 765

            lstBoxCity.Items.Add("Albany")
            lstBoxCity.Items.Add("Alexandria")
            lstBoxCity.Items.Add("Amboy")
            lstBoxCity.Items.Add("Anderson")
            lstBoxCity.Items.Add("Arlington")
            lstBoxCity.Items.Add("Atlanta")
            lstBoxCity.Items.Add("Attica")
            lstBoxCity.Items.Add("Battle Ground")
            lstBoxCity.Items.Add("Bloomingdale")
            lstBoxCity.Items.Add("Bloutsville")
            lstBoxCity.Items.Add("Buck Creek")
            lstBoxCity.Items.Add("Bunker Hill")
            lstBoxCity.Items.Add("Burlington")
            lstBoxCity.Items.Add("Cambridge City")
            lstBoxCity.Items.Add("Carthage")
            lstBoxCity.Items.Add("Cayuga")
            lstBoxCity.Items.Add("Centerville")
            lstBoxCity.Items.Add("Chesterfield")
            lstBoxCity.Items.Add("Clarks Hill")
            lstBoxCity.Items.Add("Clinton")
            lstBoxCity.Items.Add("Cloverdale")
            lstBoxCity.Items.Add("Coatesville")
            lstBoxCity.Items.Add("Colfax")
            lstBoxCity.Items.Add("Connersville")
            lstBoxCity.Items.Add("Covington")
            lstBoxCity.Items.Add("Crawfordsville")
            lstBoxCity.Items.Add("Cutler")
            lstBoxCity.Items.Add("Dana")
            lstBoxCity.Items.Add("Darlington")
            lstBoxCity.Items.Add("Delphi")
            lstBoxCity.Items.Add("Denver")
            lstBoxCity.Items.Add("Dunkirk")
            lstBoxCity.Items.Add("Eaton")
            lstBoxCity.Items.Add("Elwood")
            lstBoxCity.Items.Add("Eminence")
            lstBoxCity.Items.Add("Fairmount")
            lstBoxCity.Items.Add("Farmland")
            lstBoxCity.Items.Add("Fillmore")
            lstBoxCity.Items.Add("Fountain City")
            lstBoxCity.Items.Add("Fowler")
            lstBoxCity.Items.Add("Frankfort")
            lstBoxCity.Items.Add("Frankton")
            lstBoxCity.Items.Add("Gaston")
            lstBoxCity.Items.Add("Geetingsville")
            lstBoxCity.Items.Add("Glenwood")
            lstBoxCity.Items.Add("Greencastle")
            lstBoxCity.Items.Add("Greens Fork")

        End If




    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        'Calculate Random Prefix based upon selected city

        If lstBoxCity.SelectedItem.ToString = "Bethany" Then

            Dim Bethany As String() = {"298", "342", "443", "644", "712", "755", "759", "777", "779", "847"}
            Dim randomBethany As String = Bethany(CInt(Rnd(Bethany.Count)))

            MsgBox(randomBethany.ToString)

        End If
    End Sub
End Class

2 个答案:

答案 0 :(得分:2)

Rnd方法始终返回0到1之间的数字。如果您想要更高的数字,则需要将其乘以某个值。但是,您确实应该使用Random类来生成随机数。 Rnd方法是一种旧的VB6风格方法,主要用于向后兼容。 Random类的语法也更容易,您可以在其中指定希望结果下降的数字范围。

Dim r As New Random()
Dim randomBethany As String = Bethany(r.Next(0, Bethany.Count - 1))

但是,您应该在表单加载时创建一次Random对象,因为每次创建新表时都会重新生成随机数生成算法。

答案 1 :(得分:2)

您需要在选择随机数之前致电Randomize();否则你每次都会得到相同的随机数,因为种子是相同的,如下:

' Initialize the random-number generator.
Randomize()

Dim Bethany As String() = {"298", "342", "443", "644", "712", "755", "759", "777", "779", "847"}

' Generate random value between 1 and then length of your Bethany array
Dim randomBethany As String = Bethany(Int((Bethany.Count * Rnd()) + 1))

阅读Rnd Function (Visual Basic)了解详情。