Imports System Imports CenterSpace.NMath.Core Namespace CenterSpace.NMath.Examples.VisualBasic A .NET example in Visual Basic showing how to create a random number generator for a particular probability distribution using a specified uniform deviate method. Also shown is how to access and modify the uniform deviate generating object from the NMath Core random number generator object. Module AdvancedRNGExample Sub Main() Console.WriteLine() Create a RandomNumberGenerator.UniformRandomNumber delegate object from the method System.Random.NextDouble(). Dim SysRand As New Random() Dim UniformDeviates As RandomNumberGenerator.UniformRandomNumber = AddressOf SysRand.NextDouble Now, construct a binomial random number generator using this delegate to generate uniformly distributed random deviates between 0 and 1 (the binomial generator will transform these uniform deviates into binomial deviates). Dim Trials As Integer = 2000 Dim Prob As Double = 0.002 Dim BinRand As New RandGenBinomial(Trials, Prob, UniformDeviates) Create a vector of binomial deviates and use it to create a histogram. Dim N As Integer = 200 Dim RandomVec As New DoubleVector(N, BinRand) Create histogram Dim NumBins As Integer = 20 Dim H As New Histogram(NumBins, RandomVec) Console.WriteLine(H.StemLeaf()) Change the uniform deviate generator to use the method NextDouble() form NMath Cores RandGenMTwist class. Dim Seed As Integer = 999 Construct the MT generator with the given seed. Dim MT As New RandGenMTwist(Seed) Create the delegate. UniformDeviates = AddressOf MT.NextDouble Use the delegate to generate the uniform deviates necessary to for the binomial generator. BinRand.UniformDeviateMethod = UniformDeviates Generate a vector of binomial deviates. Dim RandomSequence1 As New DoubleVector(N, BinRand) All NMathCore random number generator classes have Reset() and Reset(int) methods that attempt to reset the underlying uniform generator with the time of day, for the no argument reset, or the given seed, for the integer argument version. These methods return true if the reset was successful and false if it was not. The reset methods will succeed if the following conditions are met: 1. The uniform generator delegate is an instance method, i.e. the Target property of the Delegate class returns a non-null reference. 2. The object reference thus obtained has a method named Initialize that returns void and takes no arguments, for the Reset() method, or a single integer argument for the Reset(int) method. The RandGenMTwist class has an Initialize(int) method, so the Reset(int) method should succeed. If (BinRand.Reset(Seed)) Then Random sequences started using the same seed, so they should be identical. Dim RandomSequence2 As New DoubleVector(N, BinRand) Console.WriteLine("randomSequence1 == randomSequence2 is {0}", RandomSequence1.Equals(RandomSequence2)) Else Console.WriteLine("Could not reset generator") End If Console.WriteLine() Console.WriteLine("Press Enter Key") Console.Read() End Sub End Module End Namespace← All NMath Code Examples