【问题标题】:Normal Distributed Random Number in VB.NETVB.NET中的正态分布随机数
【发布时间】:2013-10-08 06:08:43
【问题描述】:

有没有人知道如何在 vb.net 中制作正态分布的随机数?

谢谢

【问题讨论】:

    标签: vb.net random normal-distribution


    【解决方案1】:

    来自this forum post

    用法:

    GaussNumDist(Mean, Standard Deviation, Sample Size)
    

    下面的代码示例,它将用数字样本填充GaussNumArray(),其分布将具有指定的均值和标准差:

    Imports System.Math
    
    Module Module1
        Friend GaussNumArray() As Double
        Friend intICell As Long
    
        Friend Function GaussNumDist(ByVal Mean As Double, ByVal StdDev As Double, ByVal SampleSize As Integer)
            intICell = 1                'Loop variable
    
            ReDim GaussNumArray(SampleSize)
    
            Do While (intICell < (SampleSize + 1))
                Call NumDist(Mean, StdDev)
                Application.DoEvents()
            Loop
        End Function
    
        Sub NumDist(ByVal meanin As Double, ByVal sdin As Double)
            '---------------------------------------------------------------------------------
            'Converts uniform random numbers over the region 0 to 1 into Gaussian distributed
            'random numbers using Box-Muller algorithm.
            'Adapted from Numerical Recipes in C
            '---------------------------------------------------------------------------------
    
            'Defining variables
            Dim dblR1 As Double
            Dim dblR2 As Double
            Dim mean As Double
            Dim var As Double
            Dim circ As Double
            Dim trans As Double
            Dim dblY1 As Double
            Dim dblY2 As Double
            Dim Pi As Double
            Pi = 4 * Atan(1)
    
            'Get two random numbers
            dblR1 = (2 * UniformRandomNumber()) - 1
            dblR2 = (2 * UniformRandomNumber()) - 1
    
            circ = (dblR1 ^ 2) + (dblR2 ^ 2)        'Radius of circle
    
            If circ >= 1 Then       'If outside unit circle, then reject number
                Call NumDist(meanin, sdin)
                Exit Sub
            End If
    
            'Transform to Gaussian
            trans = Sqrt(-2 * Log(circ) / circ)
    
            dblY1 = (trans * dblR1 * sdin) + meanin
            dblY2 = (trans * dblR2 * sdin) + meanin
    
            GaussNumArray(intICell) = dblY1   'First number
    
            'Increase intICell for next random number
            intICell = (intICell + 1)
    
            GaussNumArray(intICell) = dblY2   'Second number
    
            'Increase intICell again ready for next call of ConvertNumberDistribution
            intICell = (intICell + 1)
    
        End Sub
    
        Friend Function UniformRandomNumber() As Double
            '-----------------------------------------------------------------------------------
            'Outputs random numbers with a period of > 2x10^18 in the range 0 to 1 (exclusive)
            'Implements a L'Ecuyer generator with Bays-Durham shuffle
            'Adapted from Numerical Recipes in C
            '-----------------------------------------------------------------------------------
    
            'Defining constants
            Const IM1 As Double = 2147483563
            Const IM2 As Double = 2147483399
            Const AM As Double = (1.0# / IM1)
            Const IMM1 As Double = (IM1 - 1.0#)
            Const IA1 As Double = 40014
            Const IA2 As Double = 40692
            Const IQ1 As Double = 53668
            Const IQ2 As Double = 52774
            Const IR1 As Double = 12211
            Const IR2 As Double = 3791
            Const NTAB As Double = 32
            Const NDIV As Double = (1.0# + IM1 / NTAB)
            Const ESP As Double = 0.00000012
            Const RNMX As Double = (1.0# - ESP)
    
            Dim iCell As Integer
            Dim idum As Double
            Dim j As Integer
            Dim k As Long
            Dim temp As Double
    
            Static idum2 As Long
            Static iy As Long
            Static iv(NTAB) As Long
    
            idum2 = 123456789
            iy = 0
    
            'Seed value required is a negative integer (idum)
            Randomize()
            idum = (-Rnd() * 1000)
    
            'For loop to generate a sequence of random numbers based on idum
            For iCell = 1 To 10
                'Initialize generator
                If (idum <= 0) Then
                    'Prevent idum = 0
                    If (-(idum) < 1) Then
                        idum = 1
                    Else
                        idum = -(idum)
                    End If
                    idum2 = idum
                    For j = (NTAB + 7) To 0
                        k = ((idum) / IQ1)
                        idum = ((IA1 * (idum - (k * IQ1))) - (k * IR1))
                        If (idum < 0) Then
                            idum = (idum + IM1)
                        End If
                        If (j < NTAB) Then
                            iv(j) = idum
                        End If
                    Next j
                    iy = iv(0)
                End If
    
                'Start here when not initializing
                k = (idum / IQ1)
                idum = ((IA1 * (idum - (k * IQ1))) - (k * IR1))
                If (idum < 0) Then
                    idum = (idum + IM1)
                End If
                k = (idum2 / IQ2)
                idum2 = ((IA2 * (idum2 - (k * IQ2))) - (k * IR2))
                If (idum2 < 0) Then
                    idum2 = idum2 + IM2
                End If
                j = (iy / NDIV)
                iy = (iv(j) - idum2)
                iv(j) = idum
                If (iy < 1) Then
                    iy = (iy + IMM1)
                End If
                temp = AM * iy
                If (temp <= RNMX) Then
                    'Return the value of the random number
                    UniformRandomNumber = temp
                End If
            Next iCell
        End Function
    End Module
    

    【讨论】:

    • 我已经尝试过您在上面分享的代码。它工作正常,我可以制作具有自定义大小、平均值和标准偏差的随机数。然后我向谷歌询问有关正态分布随机数的另一个参考,并找到了这个链接 [链接] (codeproject.com/Articles/15102/…)。不幸的是,我分享的链接上的代码是用 C# 制作的。有没有机会在我分享给我的程序的链接上使用正态分布代码,它是用 VB.NET 先生 @Roger Rowland 编写的?
    • @mano - 你可以convert it here,如果我的回答有帮助,也许你可以通过点击复选标记将其变为绿色和/或投票来接受它。
    【解决方案2】:

    您可以使用以下行

    Dim x1 as Double = MathNet.Numerics.Distributions.Normal.Sample(MEAN, STDEV)
    

    Math.Net Numeric 包可以使用以下 NuGet 命令安装

    Install-Package MathNet.Numerics -Version 4.9.0
    

    您可以在NuGet site找到更多信息

    【讨论】:

      猜你喜欢
      • 2017-07-24
      • 1970-01-01
      • 2011-03-17
      • 2011-12-27
      • 2014-07-13
      • 2015-03-05
      • 1970-01-01
      • 2018-06-22
      • 2020-05-05
      相关资源
      最近更新 更多