473,322 Members | 1,307 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 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 2452
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.