472,958 Members | 2,803 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

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 2436
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Jeff Thies | last post by:
I'd like to randomly sort an array. A good method?
3
by: Gaffer | last post by:
Hello Is there a way in which I can make certain parts of Html on my website random so that each viewer will see different material if they refresh the page or come back onto the website later?...
17
by: Steve Jorgensen | last post by:
If you've ever employed custom error numbers and messages in you programs, you've probably ended up with code similar to what I've ended up with in the past something like... <code> public...
1
by: Ellen Manning | last post by:
I've got an A2K database with a report that generates any number of random medical record numbers. The user inputs how many numbers they want and report uses the Randomizer function found on "The...
3
by: Rob South | last post by:
Hi All, I have an Access DB of Questions, Answers, and Distractors. The field names are as follows: Question, Choice1, Choice2, Choice3, Choice4, CorrectAnswer Choice1 at present contains...
4
by: Mr. x | last post by:
Hello, randomize is a function in vbscript. What is the function for vb ? (I have tried to use that command in the script in web-service of .net, which used VB and not VBScript). Thanks :)
7
by: Ant | last post by:
Hi, I'm using the Random class to return 5 random numbers which then are added to a string. When I do it in a controls event, such as button_ click, the numbers are random as expected, but when...
1
by: VBSTUDENT | last post by:
I am just wondering if there is a way to randomize the aritmetic operators in code, I know how to randomize numbers but I am not sure if it is possible to randomize operators. Any help would be...
5
by: gggram2000 | last post by:
Hi, I'ved spent two full days trying to find a solution, I can randomize numbers between two ranges and it works fine, But my problem is when i want to randomize five numbers that I got. eg. I...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.