473,625 Members | 2,853 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Random Numbers

How do you get random numbers in vb .net?
I want random six digit numbers.

Cheers,
Abhi
Dec 13 '05 #1
9 8375
abhi wrote:
How do you get random numbers in vb .net?
I want random six digit numbers.

Cheers,
Abhi

Does anyone even try using a search engine anymore? The second item
aftger a "vb.net random number" search is:

http://authors.aspalliance.com/brett...rsInVB.NET.asp

Random Integers

Once the class has been instantiated, a random integer can be obtained
by calling the Next method of the Random class:

http://authors.aspalliance.com/brett...rsInVB.NET.asp

Random numbers may be generated in the .NET Framework by making use of
the Random class. This class may be instantiated using the following code:

'Create a new Random class in VB.NET
Dim RandomClass As New Random()

'VB.NET
Dim RandomNumber As Integer
RandomNumber = RandomClass.Nex t()

The value of RandomNumber will, therefore, be assigned a random whole
number between 1 and 2,147,483,647.

In most coding situations, it is more desirable to create a random
number within a certain size range. In this case, the Next method should
be called with two arguments: the minimum value and the maximum value.
For example, the following assigns RandomNumber to a value that is
greater or equal to 4 and less than 14:

'VB.NET
Dim RandomNumber As Integer
RandomNumber = RandomClass.Nex t(4, 14)

Note that an ArgumentOutOfRa ngeException will be raised if the minimum
value is larger than the maximum value.

It is also possible to specify just the maximum value using a different
constructor. The following will return a return a random integer that is
greater or equal to 0 and less than 14:

'VB.NET
Dim RandomNumber As Integer
RandomNumber = RandomClass.Nex t(14)

Again, an ArgumentOutOfRa ngeException will be raised if the maximum
value is smaller than 0.
Dec 13 '05 #2
"abhi" <ab**@discussio ns.microsoft.co m> schrieb:
How do you get random numbers in vb .net?
I want random six digit numbers.


\\\
Private m_Random As New Random()
..
..
..
Dim RandomNumber As Integer = _
m_Random.Next(1 00000, 1000000)
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Dec 13 '05 #3
Here's the one that I use, which I got off of MSDN. (I needed something that was really random for a card game I was working on "for fun.") Hope this helps!

''' <summary>
''' This method simulates returns a value from 0 to n-1. The input parameter is the
''' number of sides of the dice.
''' This code is taken straight from the MSDN topic on RNGCryptoServic eProvider()
''' </summary>
''' <param name="NumSides" >Number of possible values which can be chosen</param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function GenRandomNumber (ByVal NumSides As Integer) As Integer
' Create a byte array to hold the random value.
Dim randomNumber(0) As Byte

' Create a new instance of the RNGCryptoServic eProvider.
Dim Gen As New Security.Crypto graphy.RNGCrypt oServiceProvide r()

' Fill the array with a random value.
Gen.GetBytes(ra ndomNumber)

' Convert the byte to an integer value to make the modulus operation easier.
Dim rand As Integer = Convert.ToInt32 (randomNumber(0 ))

' Return the random number mod the number
' of sides. The possible values are zero-
' based.
Return rand Mod NumSides
End Function

--Matt--*

-----Original Message-----
From: abhi
Posted At: Tuesday, December 13, 2005 1:49 PM
Posted To: microsoft.publi c.dotnet.langua ges.vb
Conversation: Random Numbers
Subject: Random Numbers
How do you get random numbers in vb .net?
I want random six digit numbers.

Cheers,
Abhi
Dec 13 '05 #4
"abhi" <ab**@discussio ns.microsoft.co m> schrieb
How do you get random numbers in vb .net? I want random six digit
numbers.

Menu Edit -> Find & Replace -> Find Symbol: Random
Armin
Dec 13 '05 #5
You probably should know... they aren't really random numbers. It's
all a facade. Reseed with the same seed each time you get three
numbers, then reseed and you'll always get the same three numbers... :)

Dec 14 '05 #6
"Snozz" <sh******@cs.fs u.edu> schrieb
You probably should know... they aren't really random numbers. It's
all a facade. Reseed with the same seed each time you get three
numbers, then reseed and you'll always get the same three numbers...
:)


I know. If you really want random numbers, connect an external sensor
measuring the cosmological radiation. ;-)
Armin

Dec 14 '05 #7
Seeding with some component of time works well in most cases.

Snozz wrote:
You probably should know... they aren't really random numbers. It's
all a facade. Reseed with the same seed each time you get three
numbers, then reseed and you'll always get the same three numbers... :)


Dec 14 '05 #8
Armin,

Or better yet, I'm sure there are web services out there that allow you
get atmospheric observations from many different weather stations.
Pull the down the temperature, dewpoint, wind speed, barometric
pressure, etc. from several stations and form a seed from that data.
The atmosphere is an inherently chaotic system. You have to admit, it
would certainly be more feasible than connecting a hardware device that
measures radiation :)

Brian

Armin Zingler wrote:
"Snozz" <sh******@cs.fs u.edu> schrieb
You probably should know... they aren't really random numbers. It's
all a facade. Reseed with the same seed each time you get three
numbers, then reseed and you'll always get the same three numbers...
:)


I know. If you really want random numbers, connect an external sensor
measuring the cosmological radiation. ;-)
Armin


Dec 14 '05 #9
Right, seed with time. My suggestion to reseed with the same seed was
only for exemplifying that the pseudo random number generator was fully
deterministic.

Dec 28 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
11945
by: Nicholas Geraldi | last post by:
Im looking for a decent random number generator. Im looking to make a large number of random numbers (100 or so, if not more) in a short period of time (as fast as possible). the function i was using to get random numbers was Random rn = new Random(System.currentTimeMillis()); but it seems that the system doesn't update the milliseconds often enough to cause a true randomaziation (i ran just the System.currentTimeMillis() in a
3
7371
by: Joe | last post by:
Hi, I have been working on some code that requires a high use of random numbers within. Mostly I either have to either: 1) flip a coin i.e. 0 or 1, or 2) generate a double between 0 and 1. I have utilised the following random number source code http://www.agner.org/random/ What I have found is that there is a problem with seeding. The code generates a seed based on time(0). I have found that I need to increment
21
3007
by: Marc Dansereau | last post by:
Hi all I am new to this forum and to the c programming language. If I understand, the random() function in C return numbers that follow a uniform distribution U(0,1). Can somebody know how to generate a set of random number that follow a normal distribution N(0,1) ? I am develloping on power4 machine running AIX. Thank you for your help
5
2391
by: cvnweb | last post by:
I am trying to generate 2 random numbers that are diffrent, in order to add them to existing numbers to generate numbers that start out the same, but are randomly added and subtracted so that they can go down similar paths, but not be the same. I will implement code later to make sure i they go more than 10 apart from each other that they are moved closer together, but this is what I have so far, and when the program is run, the two random...
104
5120
by: fieldfallow | last post by:
Hello all, Is there a function in the standard C library which returns a prime number which is also pseudo-random? Assuming there isn't, as it appears from the docs that I have, is there a better way than to fill an array of range 0... RAND_MAX with pre-computed primes and using the output of rand() to index into it to extract a random prime.
12
5217
by: Jim Michaels | last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or mysql. I have not been able to do either. I get the same number back several times from PHP's mt_rand() and from mysql's RAND(). any ideas? I suppose I could use the current rancom number as the seed for the next random number. but would that really work?
21
13500
by: chico_yallin | last post by:
I just wana make a random id number based on4 digits-for examples?? Thanks in Advance Ch.Yallin
13
2798
by: Peter Oliphant | last post by:
I would like to be able to create a random number generator that produces evenly distributed random numbers up to given number. For example, I would like to pick a random number less than 100000, or between 0 and 99999 (inclusive). Further, the I want the range to be a variable. Concretely, I would like to create the following method: unsigned long Random( unsigned long num )
6
11741
by: badcrusher10 | last post by:
Hello. I'm having trouble figuring out what to do and how to do.. could someone explain to me what I need to do in order to work? THIS IS WHAT I NEED TO DO: Professor Snoop wants a program that will randomly generate 10 unique random numbers. Your job is to write a program that produces random permutations of the numbers 1 to 10. “Permutation” is a mathematical name for an arrangement. For example, there are six permutations of the...
24
7202
by: pereges | last post by:
I need to generate two uniform random numbers between 0 and 1 in C ? How to do it ? I looked into rand function where you need to #define RAND_MAX as 1 but will this rand function give me uniformly distributed and unique numbers ?
0
8253
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8189
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
8692
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...
0
7182
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...
0
5570
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
4089
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...
0
4192
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2621
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
1
1802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.