使用正态分布创建数组

时间:2017-03-31 16:32:40

标签: vba excel-vba excel

我正在尝试创建一个大小为N的vba数组,正常分布的百分比增加100%。这意味着输入一个整数N,我应该得到一个n大小的数组,其值为100,并且是正态分布的

我该怎么做?

谢谢!

2 个答案:

答案 0 :(得分:2)

我很好奇所以我做了一个。

Sub NDArray()
Dim arr() As Double
a = InputBox("Number of numbers")

ReDim arr(a - 1) As Double
With Application.WorksheetFunction
    'fill the array with random numbers that fit a normal dist
    For i = 0 To a - 1
        '"A/100" is the target mean and "A/200" is target Std. Dev, change to your desire
        arr(i) = .Norm_Inv(.RandBetween(1, 1000) / 10000, a / 100, a / 200)
    Next i
    'change the numbers to percentage of whole and multiply by 100 to get the values to sum to 100
    x = .Sum(arr)
    For i = 0 To a - 1
        arr(i) = (arr(i) / x) * 100
    Next i
    'post the numbers in Column A on the active sheet
    ActiveSheet.Range("A:A").ClearContents
    ActiveSheet.Range("A1").Resize(a).Value = .Transpose(arr)
End With
End Sub

答案 1 :(得分:0)

尝试以下方法:

Sub NDArray()
a = InputBox("enter the integer")
ReDim arr(a - 1)
For i = 0 To a - 1
arr(i) = 100 / a
Next i
End Sub

但我同意Scott的意见......尝试先编写代码并发布您的问题......

相关问题