By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,395 Members | 2,479 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,395 IT Pros & Developers. It's quick & easy.

randomize 2 different numbers in same subroutine?

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
"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.