Jump to content

Generate an array of 20 random numbers between 1-100...

- - - - -

  • Please log in to reply
3 replies to this topic

#1
Surfensteve

Surfensteve

    Newbie

  • Members
  • Pip
  • 5 posts
Hello, I am trying to generate an array of 20 random numbers between 1-100 in a list box. I have not yet been successful to get it to work at the moment. I viewed a few tutorials and youtube videos but are not that helpful for what I am doing. What am I missing? How can I get it to work?

-Steve

Here is my code so far...




Public Class Form1

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

Dim x As Double
Dim i As Integer
Dim SampleArray(20) As Double
Randomize()
For i = 0 To 20
SampleArray(i) = Rnd() * 100
Next



ListBox1.Items.Add(i)

End Sub
End Class

#2
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
Your random generation is working fine its just your printing "i" into your listBox, which you dont want. You want the array value at position "i".
Dim i As Integer
        Dim SampleArray(20) As Double
        Randomize()
        For i = 0 To 20
            SampleArray(i) = Rnd() * 100
    [COLOR=red]        ListBox1.Items.Add(SampleArray(i).ToString)[/COLOR]
        Next

You may notice that creates some very long random numbers. You may find this of help,
Generating Random Numbers in the .NET Framework
&
        Dim randomNum As New Random()
        Dim i As Integer
        Dim SampleArray(20) As Double
      For i = 0 To 20
        SampleArray(i) = randomNum.Next(1, 100)
        ListBox1.Items.Add(SampleArray(i).ToString)
      Next

~ Committed. :)
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.

#3
Surfensteve

Surfensteve

    Newbie

  • Members
  • Pip
  • 5 posts
Thanks CommittedC0der, I am still learning visual basic so I am still not always seeing the light of what I am doing wrong when writing code.

#4
SimonCoder

SimonCoder

    Newbie

  • Members
  • Pip
  • 8 posts
Here is the code that I use to generate random strings with. I like using this because I can call the generate function multiple times during my code and set the number of generated values independantly.
    Function GenerateRandomString(ByRef length As Integer) As String

        Randomize()

        Dim allowableChars As String

        allowableChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"


        Dim i As Integer

        For i = 1 To length

            GenerateRandomString = GenerateRandomString & Mid$(allowableChars, Int(Rnd() * Len(allowableChars) + 1), 1)

        Next

    End Function

Just change the "allowableChars" to just numbers if you just want random numbers instead of Char's and Num's.

Call the function using: GenerateRandomString(10) (Will generate 10 random char's and numb's)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users