Randm variable woes

tamal_patra

New member
Joined
Feb 9, 2006
Messages
1
Programming Experience
Beginner
:confused: I can't use the "random" variables properly. When it is set to choose from 2 or 3 choices, it always return any fixed result from the choices everytime. How can I get it(the random variable) to return different value even for 2 or three choice?
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,348
Location
Sydney, Australia
Programming Experience
10+
The thing to do would be to post the code you're currently using so that we can see what you're doing wrong. Here's an example of getting 10 random choices from a group of 3:
VB.NET:
Dim values(10 - 1) As Integer 'Create an array of 10 Integers.
Dim rand As New Random 'Create the random number generator.

'Fill the array with random numbers from 1 to 3.
For i As Integer = 0 To values.GetUpperBound(0) Step 1
    values(i) = rand.Next(1, 3 + 1)
Next i
You should pay specific attention to what the arguments to Next mean. The number returned will be greater than or equal to the first argument and less than the second. That's why the second argument has to be one more than the maximum value you want returned. I like to use the (+ 1) notation to make this fact clear.
 
Top Bottom