I try to generate 10 random integers and store it in an array(9). Then I split the array into half by converting it into string. My code can compile but it sometimes work and most time cannot work. I don't know what is the problem....

Public Class Form1

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

        Randomize()
        Dim rand As New Random
        Dim array(9) As String
        Dim X As String
        Dim Y As String
        Dim i As Integer

        For i = 0 To 9
            array(i) = rand.Next
            i = i + 1
        Next

        Dim HiOrder As String = array(0).Substring(0, 5)
        Dim LoOrder As String = array(0).Substring(5, 5)

        X = 1.3 + (HiOrder * ((2.4 - 1.3) / ((10 ^ 5) - 1)))
        Y = 3.4 + (LoOrder * ((4.7 - 3.4) / ((10 ^ 5) - 1)))

        ListBox1.Items.Add(array(0))
        ListBox1.Items.Add(HiOrder)
        ListBox1.Items.Add(LoOrder)
        ListBox1.Items.Add("X = " & X)
        ListBox1.Items.Add("Y = " & Y)

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub

End Class

Can help me with this??? I can't seem to get it to run every time....

Dani AI

Generated

There are three things causing the "sometimes works" behavior: the loop modifies its counter inside the body so array slots are skipped or left unset, the random integer you turn into a string can have fewer than ten digits so any fixed Substring calls sometimes fail, and numeric math is being done while variables are declared as strings (implicit conversions are fragile). and were right to point at the length/substring issue — the safe fix is to either force a fixed-length string or avoid slicing a single large integer and instead generate the two 5-digit components explicitly.

A straightforward, robust rewrite:

' put this at form scope so the generator is not reseeded each click
Private rnd As New Random()

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    ' store numeric values rather than strings
    Dim numbers(9) As Integer
    For i As Integer = 0 To numbers.Length - 1
        numbers(i) = rnd.Next()          ' fill array
    Next

    ' if you need two independent 5-digit parts, generate them directly
    Dim hiPart As Integer = rnd.Next(0, 100000)   ' 0..99999
    Dim loPart As Integer = rnd.Next(0, 100000)

    Dim X As Double = 1.3 + hiPart * ((2.4 - 1.3) / (100000 - 1))
    Dim Y As Double = 3.4 + loPart * ((4.7 - 3.4) / (100000 - 1))

    ListBox1.Items.Add(numbers(0).ToString())
    ListBox1.Items.Add(hiPart.ToString("D5"))     ' shows leading zeros if any
    ListBox1.Items.Add(loPart.ToString("D5"))
    ListBox1.Items.Add("X = " & X)
    ListBox1.Items.Add("Y = " & Y)
End Sub

Notes and troubleshooting:

  • Remove any manual increment of the For counter; let the loop control the index.
  • If you really must slice a single number, convert it to a fixed-length string (pad with zeros) before Substring, and use Integer.TryParse to avoid exceptions.
  • Declare X/Y as Double and parse the numeric parts before arithmetic.
  • Keep the Random instance at form scope so repeated clicks don’t accidentally reseed it. This addresses the issues raised by , and and gives two reliable fixes: fixed-length strings or generating the two parts directly.

Recommended Answers

All 2 Replies

rand.Next

will create a random number between 0 and Int32.MaxValue

so if the random number is smaller than 9999 then "array(0).Substring(0, 5)" will fail
to make sure it dont fail do

rand.Next(10000,Int32.MaxValue)

First time might be u r getting array(0) is 10 digits and when click again the button, arrya(0) holding 9 digits. So when your are trying to use substring(5,5), length is not correct thats what throwing the exception.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.