473,623 Members | 2,447 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2481
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**@discussio ns.microsoft.co m> 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
4158
by: Jeff Thies | last post by:
I'd like to randomly sort an array. A good method?
3
14625
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? I am currently using the "<!--#include virtual="cgi-bin/menu1.txt" -->" command to display my menus on every page of my site, so that the same material is shown on every page, and I would like a way that I can randomize this perhaps. I got a...
17
7450
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 Const xyzcErrNumBase As Long = 500 Public Const xyzcErrNumHeyDontDoThat = xyzcErrNumBase + 1 Public Const xyzcErrTxtHeyDontDoThat = "Hey! Don't do that!"
1
3419
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 Access Web" site to generate these numbers. Out of 38000 records, there are 900 unique medical record numbers. I run the randomizer function on a query of these unique medical record numbers. The user states there are too many duplications...
3
1784
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 the correct answer. Choices 2-4 are distractors. CorrectAnswer denotes which of Choice1-4 is the correct answer.
4
6694
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
2014
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 I call the random function as a method of a class, all the numbers are the same for each call to the class. For example: (Button click calls the myRandomObject.ReturnValue method) button click produces 5,5,5
1
2150
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 appreciated Thanks
5
2885
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 randomize 1-100 and I get 50. I randomize 1-900 on another input field and I get 578. I randomize 2-56 and get 31. " " and get 43. " " and get 78. So I have five numbers: 50, 578, 31, 43 and 78.
0
8168
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8672
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8330
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7153
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6107
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5561
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4075
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2603
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1474
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.