Computing.Net > Forums > Programming > random number in visual basic

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

random number in visual basic

Reply to Message Icon

Name: Sean
Date: July 19, 2002 at 09:32:03 Pacific
Comment:

I know that to get a random number it's just Math.Random(int)

But that gives me one random number, what I would like is a function that could get me an array of random numbers, for example:

Random of 6 #s: 1,3,4,5,0,2



Sponsored Link
Ads by Google

Response Number 1
Name: Burbble
Date: July 19, 2002 at 14:02:40 Pacific
Reply:

I'm not exactly sure how to use the Math.Random method you are referring to, but here's how you could randomize an an array of numbers:

Not the most efficient way, though...

First, make a list box on the form called List1 and put this code into a Command button:


Dim RndNums(5) As Integer
Dim Rnd1 As Integer
Dim Counter1 As Integer

Private Sub Command1_Click()
List1.Clear
For i = 0 To 5
List1.AddItem i
Next i

Counter1 = 0
Do Until Counter1 = 6
Rnd1 = Int(Rnd * List1.ListCount)
RndNums(Counter1) = List1.List(Rnd1)
List1.RemoveItem Rnd1
Counter1 = Counter1 + 1
Loop

For i = 0 To 5
List1.AddItem RndNums(i)
Next i
End Sub


-Burbble


0

Response Number 2
Name: Somebody
Date: July 21, 2002 at 12:03:49 Pacific
Reply:

this short line of code does thesame thing as the code mentioned above (i've used thesame form with the cmdbutton and the list)

Private Sub Command1_Click()

List1.Clear
Dim a As Integer
For i = 1 To 6
a = Int(Rnd * 10) + 1
List1.AddItem a
Next i

End Sub


0

Response Number 3
Name: Somebody
Date: July 21, 2002 at 12:06:33 Pacific
Reply:

Forgot to mention that this one genarates numbers between 10 and 1. To generate numbers between 2 other numbers, replace 10 with the highest number and 1 with the lowest number.


0

Response Number 4
Name: Burbble
Date: July 22, 2002 at 06:35:27 Pacific
Reply:

They are not the same thing; examine them more closely.

-Burbble


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Need commands in VBE 6.0 ... Combo Boxes



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: random number in visual basic

random numbers in Quick Basic www.computing.net/answers/programming/random-numbers-in-quick-basic/12916.html

Randomizing Numbers in VB6 www.computing.net/answers/programming/randomizing-numbers-in-vb6/673.html

Caller ID in Visual Basic www.computing.net/answers/programming/caller-id-in-visual-basic/11535.html