C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Argument 1: This is the minimum value returned by Next. It is inclusive, so can be a possible result.
Argument 2: This is the exclusive (not included) maximum. A max of 3 means the highest result possible is 2.
VB.NET program that uses random, Next
Module Module1
Sub Main()
Dim r As Random = New Random
' Get random numbers between 1 and 3.
' ... The values 1 and 2 are possible.
Console.WriteLine(r.Next(1, 3))
Console.WriteLine(r.Next(1, 3))
Console.WriteLine(r.Next(1, 3))
End Sub
End Module
Output
1
2
1
Here: The program writes three random numbers to the screen. The output will vary each time you run it.
VB.NET program that uses field, module
Module Module1
Sub Main()
' Write three random numbers.
F()
F()
F()
End Sub
''' <summary>
''' Write the next random number generated.
''' </summary>
Private Sub F()
' Call Next method on the random object instance.
Console.WriteLine(_r.Next)
End Sub
''' <summary>
''' Store a random number generator.
''' </summary>
Private _r As Random = New Random
End Module
Output
1284029964
984949494
1322530626
Info: A loop that generates each byte individually could be used, but NextBytes is clearer and simpler to read.
VB.NET program that generates random bytes with NextBytes
Module Module1
Sub Main()
Dim randomGenerator = New Random()
' Array of 10 random bytes.
Dim buffer(9) As Byte
' Generate 10 random bytes.
randomGenerator.NextBytes(buffer)
For Each value In buffer
Console.Write("[{0}]", value)
Next
End Sub
End Module
Output
[250][186][58][0][205][149][152][237][30][26]
Review: The first argument is the inclusive lower bound. The second argument is the exclusive upper bound.
However: If no arguments are passed, any positive number is returned. If one argument is used, that is the max.