2017-03-31 109 views
-4

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

我该怎么做?

谢谢!

+0

@ScottCraner ......我同意,但它是一个非常简单的代码,只是想正态分布部分一些帮助,我不知道该怎么办 – jhugo

+0

快速搜索的“填充阵列正常发行vba“给了很多起点。 –

回答

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 
+0

谢谢@ScottCraner ! – jhugo

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 

但我也同意斯科特...尝试先写代码,并发表您的问题...

+0

这将返回一个相同数字的数组。 OP希望它[正常分布](http://www.stat.yale.edu/Courses/1997-98/101/normal.htm)。这对于OP来说是一个好的开始,但是这不会做所要求的。 –

+1

哦,我错过了正常分布部分的问题... –

+0

非常感谢你@AvinashKumar ...我有相同的代码,与正常分布的部分是我不知道该怎么办 – jhugo