If you like this article, please click on “+1“ button and share with your friends.
Click here for the Video Tutorial of this article.
visit www.iGnani.com
The Random class defined in the .NET Framework class library provides functionality to generate random numbers.
The Random class constructors have two overloaded forms. It takes either no value or it takes a seed value.
In the NoSeedRandom method below, I am using the Random object without a Seed. A Seed is a number used to calculate a starting value for the pseudo-random number sequence. If a negative number is specified, the absolute value of the number is used.
In the next three methods, I am using the Random object first with Seed, next with Seed and Upper limit in case you want the number to be generated within the upper limit specified. And the last method with Seed and both Upper and lower limit, to make sure we get the random number within a range.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
NoSeedRandom()
NoBoundsRandoms(1000)
UpperBoundRandoms(1000, 400)
BothBoundsRandoms(100, 50, 60)
End Sub
Sub NoSeedRandom()
Console.WriteLine(vbCrLf & "Random object, without Seed, no bounds:")
Dim randObj As New Random()
' Generate Five random integers from 0 to int.MaxValue.
Dim j As Integer
For j = 0 To 4
Console.Write("{0,11} ", randObj.Next())
Next j
Console.WriteLine()
End Sub
' Generate random numbers with no bounds specified.
Sub NoBoundsRandoms(seed As Integer)
Console.WriteLine(vbCrLf & "Random object, seed = {0}, no bounds:", seed)
Dim randObj As New Random(seed)
' Generate Five random integers from 0 to int.MaxValue.
Dim j As Integer
For j = 0 To 4
Console.Write("{0,11} ", randObj.Next())
Next j
Console.WriteLine()
End Sub
' Generate random numbers with an upper bound specified.
Sub UpperBoundRandoms(seed As Integer, upper As Integer)
Console.WriteLine(vbCrLf & "Random object, seed = {0}, upper bound = {1}:", seed, upper)
Dim randObj As New Random(seed)
' Generate Five random integers from 0 to the upper bound.
Dim j As Integer
For j = 0 To 4
Console.Write("{0,11} ", randObj.Next(upper))
Next j
Console.WriteLine()
End Sub
' Generate random numbers with both bounds specified.
Sub BothBoundsRandoms( seed As Integer, lower As Integer, upper As Integer)
Console.WriteLine(vbCrLf & "Random object, seed = {0}, lower = {1}, " & "upper = {2}:", seed, lower, upper)
Dim randObj As New Random(seed)
' Generate Five random integers from the lower to
' upper bounds.
Dim j As Integer
For j = 0 To 4
Console.Write("{0,11} ", randObj.Next(lower, upper))
Next j
Console.WriteLine()
End Sub
End Class
visit www.iGnani.com
MicroMind Information Systems
#4013, K.R. Road, Banashankari II Stage,
BANGALORE - 560070
KARNATAKA, INDIA
Phone: +91 80 26762747
Technorati Tags: Random Number, NoSeedRandom, Object, System, EventArgs, Handles, NoBoundsRandoms, UpperBoundRandoms, BothBoundsRandoms, Console, WriteLine, Generate, MaxValue, MicroMind Information Systems, iGnani
Blogger Labels: Random Number, NoSeedRandom, Object, System, EventArgs, Handles, NoBoundsRandoms, UpperBoundRandoms, BothBoundsRandoms, Console, WriteLine, Generate, MaxValue, MicroMind Information Systems, iGnani
No comments:
Post a Comment