如何循环和计算平均值

如何循环和计算平均值

本文介绍了如何循环和计算平均值.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请我打算在excel上运行等效的蒙特卡洛模拟.我有一组与某些公式单元格相关的随机数字,可以给我一个答案.我已经编写了代码,并且已将其循环了10,000次.我的结果给出了第10,000次的答案,但这不是我想要的,

我想要平均10,000个答案. (即:对于每个循环,它都将答案保持悬念状态,并在10000次模拟结束时进行10、000次循环,平均得出答案.
请解决我的问题,我将不胜感激.


这是我写的代码:


please i intend to run an equivalent of a Monte Carlo simulation on excel. i have a set of random figures linked to some formula cells to give me an answer. i have written the code, and i have looped it 10,000 times. my result gives me the answer as at the 10,000th time, but this is not what i want,

I want an average of the 10,000 answers. (i.e : for every loop, it keep the answer in suspense, and does the loop 10, 000 times, at the end of the 10000 simulations, it does an average of the answers.
please i would be grateful if i my problem is solved.


Here is the code I wrote:


Sub VaRSimulation()
'
' VaRSimulation Macro
'
' Keyboard Shortcut: Ctrl+a
'
Let x = 0
Do While x < 20
    Sheets("RAND (3) exp").Select
    Range("A2").Select
    ActiveCell.FormulaR1C1 = "=RAND()"
    Range("A2").Select
    Selection.AutoFill Destination:=Range("A2:A253")
    x = x + 1
Loop
    Range("G1:G3").Select
    Selection.Copy
    Sheets("Var Analysis and result").Select
    Range("G1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Cells.Select
    Cells.EntireColumn.AutoFit
    Range("G1:G3").Activate
    Selection.Font.Bold = True
    Range("A2").Select
End Sub



现在,这只是将其循环20次,我想要的是,每个循环之后,答案都存储起来,最后,平均起来给我一个答案(PS,将有3个答案" G1:G3.对于G1,经过20次模拟后,平均需要进行20次模拟,G2进行相同,G3进行相同)"谢谢



now, what this does is just to loop it 20 times, what i want is, "after each loop, the answers are stored up and at the end, takes an average to give me an answer (P.S, there will be 3 answers "G1:G3". for G1, after 20 simulations, it takes an average of the 20 simulations, G2 does the same, and G3 does the same)" thank you

推荐答案


这篇关于如何循环和计算平均值.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 19:33