Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VB.NET中的正态分布随机数_Vb.net_Random_Normal Distribution - Fatal编程技术网

VB.NET中的正态分布随机数

VB.NET中的正态分布随机数,vb.net,random,normal-distribution,Vb.net,Random,Normal Distribution,有人知道如何在vb.net中生成正态分布随机数吗 谢谢来自: 用法: GaussNumDist(Mean, Standard Deviation, Sample Size) 下面的代码示例,将使用数字样本填充Gaussnumaray(),其分布将具有指定的平均值和标准偏差: Imports System.Math Module Module1 Friend GaussNumArray() As Double Friend intICell As Long Frien

有人知道如何在vb.net中生成正态分布随机数吗

谢谢

来自:

用法:

GaussNumDist(Mean, Standard Deviation, Sample Size)
下面的代码示例,将使用数字样本填充Gaussnumaray(),其分布将具有指定的平均值和标准偏差:

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
导入系统数学
模块1
Friend Gausnsunmaray()作为双倍
朋友和你一样长
友元函数GaussNumDist(ByVal Mean为Double,ByVal StdDev为Double,ByVal SampleSize为Integer)
intICell=1'循环变量
ReDim Gaussnumaray(样本大小)
执行时(单元格<(样本大小+1))
调用NumDist(平均值,STDEV)
Application.DoEvents()
环
端函数
子数值列表(ByVal表示双精度,ByVal表示双精度)
'---------------------------------------------------------------------------------
'将区域0到1上的均匀随机数转换为高斯分布
“使用Box-Muller算法的随机数。
'改编自C中的数字配方
'---------------------------------------------------------------------------------
“定义变量
双精度dblR1
双精度dblR2
我的意思是双重的
作为双变量的Dim var
暗圈为双圈
双精度变速器
将dblY1设置为双精度
双精度dblY2
双精度Pi
Pi=4*Atan(1)
“得到两个随机数
dblR1=(2*UniformRandomNumber())-1
dblR2=(2*UniformRandomNumber())-1
圆环=(dblR1^2)+(dblR2^2)'圆半径
如果circ>=1,则“如果在单位圆外,则拒绝编号
调用NumDist(平均值,sdin)
出口接头
如果结束
'转换为高斯
trans=Sqrt(-2*Log(circ)/circ)
dblY1=(trans*dbl1*sdin)+meanin
dblY2=(trans*dblR2*sdin)+meanin
GaussNumArray(intICell)=dblY1'第一个数字
'增加下一个随机数的单元格
intICell=(intICell+1)
GaussNumArray(intICell)=dblY2'第二个数字
'再次增加单元格,为下一次调用ConvertNumberDistribution做好准备
intICell=(intICell+1)
端接头
友元函数UniformRandomNumber()为双精度
'-----------------------------------------------------------------------------------
'输出0到1范围内周期大于2x10^18的随机数(排除)
'使用Bays Durham shuffle实现L'Ecuyer生成器
'改编自C中的数字配方
'-----------------------------------------------------------------------------------
“定义常数
常数IM1为双精度=2147483563
常数IM2为双精度=2147483399
常数AM为双精度=(1.0#/IM1)
常量IMM1为Double=(IM1-1.0#)
常数IA1为双精度=40014
常数IA2为双精度=40692
常量IQ1为双精度=53668
常量IQ2为双精度=52774
常数IR1为双精度=12211
常数IR2为双精度=3791
常数NTAB为双精度=32
双精度常数NDIV=(1.0#+IM1/NTAB)
常量ESP为双精度=0.00000012
常量RNMX为双精度=(1.0#-ESP)
Dim iCell作为整数
双碘碘
作为整数的Dim j
暗k一样长
双温变暗
静态idum2长度为
静态iy只要
静态iv(NTAB)长度相同
idum2=123456789
iy=0
'所需种子值为负整数(idum)
随机化
idum=(-Rnd()*1000)
'For循环根据idum生成随机数序列
对于iCell=1到10
'初始化生成器

如果(idum您可以使用以下行

Dim x1 as Double = MathNet.Numerics.Distributions.Normal.Sample(MEAN, STDEV)
可以使用以下NuGet命令安装Math.Net数值包

Install-Package MathNet.Numerics -Version 4.9.0

您可以在

上找到更多信息,我已经尝试了您在上面共享的代码,先生。这很好,我可以用自定义大小、平均值和标准偏差生成随机数。然后我向谷歌询问另一个关于正态分布随机数的参考,并找到了此链接[链接]()。不幸的是,我共享的链接上的代码是由C#生成的。我共享的程序链接上的正态分布代码是用VB.NET编写的吗?sir@Roger Rowland?@mano?您可以,如果我的答案有帮助,您可以通过单击复选标记使其变为绿色和/或向上投票来接受它。