• 0

[vbnet] Integer more than 9?


Question

Ok, I am making a simple Random Number Generator in VB.NET. Now, I am generating the numbers with this code:

Dim randomgenerator As New Random
Dim randomint As Integer = randomgenerator.Next(1, 8)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim randomint As Integer = randomgenerator.Next(1, 8)
        textbox2.Text = randomgenerator.Next

        input = TextBox1.Text
        If input = "1" Then
            one()
        ElseIf input = "2" Then
            two()
        ElseIf input = "3" Then
            three()
        ElseIf input = "4" Then
            four()
        ElseIf input = "5" Then
            five()
        ElseIf input = "6" Then
            six()
        ElseIf input = "7" Then
            seven()
        ElseIf input = "8" Then
            eight()
        ElseIf input = "9" Then
            nine()
        End If

    End Sub

 Private Sub two()
        Dim randomint As Integer = randomgenerator.Next(1, 8)
        randomint = randomgenerator.Next(9, 99)
        textbox2.Text = randomint
    End Sub

    Private Sub three()
        Dim randomint As Integer = randomgenerator.Next(1, 8)
        randomint = randomgenerator.Next(99, 999)
        textbox2.Text = randomint
    End Sub

    Private Sub four()
        Dim randomint As Integer = randomgenerator.Next(1, 8)
        randomint = randomgenerator.Next(999, 9999)
        textbox2.Text = randomint
    End Sub

And so on and so fourth. Now, the problem is, when I get to;

Private Sub nine()
        Dim randomint As Integer = randomgenerator.Next(1, 8)
        randomint = randomgenerator.Next(99999999, 999999999)
        textbox2.Text = randomint
    End Sub

And try to go on to ten, and add one more digit to max and min value, it won't let me, giving a "Constand Expression not representable in type Integer"

How can I get around this to generate random numbers larger than 9?

Thanks!

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

Uhm, you can't? You're going to have to generate two 5 digit integers, and piece them together, using your way. Max size of an Int32 (which I'm guessing random uses) is 2147483647.

Here. use this:

   Function GenerateRandomNumber(ByVal howmany As Integer) As String
        Dim x As Random = New Random
        Dim i As Integer
        Dim r As String
        Do While i < howmany
            r &= x.Next(1, 9)
            i += 1
        Loop
        Return r
    End Function

Link to comment
Share on other sites

  • 0
Hmmm i don't quite get why you're making ANOTHER random generator :s... it's like you're reinventing the wheel.

584789758[/snapback]

And he's using another random generator to do it :huh:

Link to comment
Share on other sites

  • 0

:|

First, what's the point of this?

      Dim randomint As Integer = randomgenerator.Next(1, 8)
       randomint = randomgenerator.Next(999, 9999)

You already assigned it a random number between 1 and 8, then you assign it a second random number between 999 and 9999 :huh: Just take out the first assignment completely.

And are you saying you want a nine-digit random number? I don't understand this.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.