Random Numbers in Visual Basic .NET

Here’s the translation of the Go code example to Visual Basic .NET, formatted in Markdown suitable for Hugo:

Our first program demonstrates the generation of random numbers. Here’s the full source code:

Imports System
Imports System.Random

Module RandomNumbers
    Sub Main()
        ' Create a new instance of the Random class
        Dim rand As New Random()

        ' For example, Next(100) returns a random Integer n,
        ' 0 <= n < 100.
        Console.Write(rand.Next(100) & ",")
        Console.WriteLine(rand.Next(100))

        ' NextDouble() returns a Double f,
        ' 0.0 <= f < 1.0.
        Console.WriteLine(rand.NextDouble())

        ' This can be used to generate random doubles in
        ' other ranges, for example 5.0 <= f' < 10.0.
        Console.Write((rand.NextDouble() * 5) + 5 & ",")
        Console.WriteLine((rand.NextDouble() * 5) + 5)

        ' If you want a known seed, create a new
        ' Random instance with a specific seed.
        Dim r2 As New Random(42)
        Console.Write(r2.Next(100) & ",")
        Console.WriteLine(r2.Next(100))

        ' Creating another Random instance with the same seed
        ' will produce the same sequence of random numbers.
        Dim r3 As New Random(42)
        Console.Write(r3.Next(100) & ",")
        Console.WriteLine(r3.Next(100))
    End Sub
End Module

To run the program, save the code in a file with a .vb extension and compile it using the Visual Basic compiler, then run the resulting executable.

$ vbc RandomNumbers.vb
$ RandomNumbers.exe
68,56
0.8090228139659177
5.840125017402497,6.937056298890035
94,49
94,49

Some of the generated numbers may be different when you run the sample.

Note that Visual Basic .NET uses the Random class from the System namespace for random number generation. Unlike the Go example, which uses a PCG (Permuted Congruential Generator), VB.NET’s Random class typically uses a different algorithm. However, the basic principle of seeded random number generation remains the same.

See the Random Class documentation for references on other random quantities that Visual Basic .NET can provide.