468,275 Members | 1,905 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,275 developers. It's quick & easy.

randomize 2 different numbers in same subroutine?

Here is what I am trying for randomizing 2 numbers in the same subroutine so
that they are not equal to each other:

Dim j As Integer, k As Integer
j = New System.Random().Next(0, 10)
k = New System.Random().Next(0, 10)

But j and k are always equal to each other. So I through in Randomize( ) but
that did not help. Is it possible to randomize 2 different numbers in the
same sub so that they are not equal to each other? How to do this?

Thanks

Nov 21 '05 #1
2 2241
do this:
Dim j, k As Integer
Dim rng As New System.Random
j = rng.Next(0, 10)
k = rng.Next(0, 10)
Your code is creating a new Random object, calling Next, and assigning j a
value. Then it does the same to assign k a variable. In the time it takes
to do this, the clock does not tick, so the two Random object instances you
create get initialize the same way, and hence return the same numbers. The
above code creates a new Random object only once.

"Rich" wrote:
Here is what I am trying for randomizing 2 numbers in the same subroutine so
that they are not equal to each other:

Dim j As Integer, k As Integer
j = New System.Random().Next(0, 10)
k = New System.Random().Next(0, 10)

But j and k are always equal to each other. So I through in Randomize( ) but
that did not help. Is it possible to randomize 2 different numbers in the
same sub so that they are not equal to each other? How to do this?

Thanks

Nov 21 '05 #2
"Rich" <Ri**@discussions.microsoft.com> schrieb:
Dim j As Integer, k As Integer
j = New System.Random().Next(0, 10)
k = New System.Random().Next(0, 10)

But j and k are always equal to each other. So I through in Randomize( )
but
that did not help. Is it possible to randomize 2 different numbers in the
same sub so that they are not equal to each other? How to do this?

'Randomize' has nothing to do with the 'Random' class. You need to call
'Randomize' before calling VB.NET's 'Rnd' function once. If you are using
the 'Random' class, one instance should be sufficient:

\\\
Dim rng As New Random()
..
..
..
i = rng.Next(...)
j = rng.Next(...)
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

21 posts views Thread by Jeff Thies | last post: by
3 posts views Thread by Gaffer | last post: by
17 posts views Thread by Steve Jorgensen | last post: by
1 post views Thread by Ellen Manning | last post: by
3 posts views Thread by Rob South | last post: by
4 posts views Thread by Mr. x | last post: by
7 posts views Thread by Ant | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by zattat | last post: by
1 post views Thread by MrBee | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.