473,388 Members | 1,322 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,388 software developers and data experts.

Random Numbers

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

Cheers,
Abhi
Dec 13 '05 #1
9 8363
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.Next()

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.Next(4, 14)

Note that an ArgumentOutOfRangeException 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.Next(14)

Again, an ArgumentOutOfRangeException will be raised if the maximum
value is smaller than 0.
Dec 13 '05 #2
"abhi" <ab**@discussions.microsoft.com> 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(100000, 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 RNGCryptoServiceProvider()
''' </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 RNGCryptoServiceProvider.
Dim Gen As New Security.Cryptography.RNGCryptoServiceProvider()

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

' 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.public.dotnet.languages.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**@discussions.microsoft.com> 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.fsu.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.fsu.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
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...
3
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...
21
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...
5
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...
104
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...
12
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...
21
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
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,...
6
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...
24
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 ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.