Random Numbers

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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

Cheers,
Abhi
 
abhi said:
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/brettb/RandomNumbersInVB.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/brettb/RandomNumbersInVB.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.
 
abhi said:
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)
///
 
abhi said:
How do you get random numbers in vb .net? I want random six digit
numbers.


Menu Edit -> Find & Replace -> Find Symbol: Random


Armin
 
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
 
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... :)
 
Snozz said:
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
 
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
 
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top