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
Generate an array of 20 random numbers between 1-100...
Started by Surfensteve, Mar 02 2011 05:18 PM
3 replies to this topic
#1
Posted 02 March 2011 - 05:18 PM
|
|
|
#2
Posted 03 March 2011 - 07:47 AM
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".
You may notice that creates some very long random numbers. You may find this of help,
Generating Random Numbers in the .NET Framework
&
~ Committed. :)
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.
Science is only an educated theory, which we cannot disprove.
#3
Posted 03 March 2011 - 01:10 PM
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
Posted 10 March 2011 - 12:37 PM
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.
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)
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


Sign In
Create Account

Back to top









