Created
October 27, 2019 18:47
-
-
Save Wolwer1nE/c9423f662f39c39dd1952587334cebbd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def integrating_Monte_Carlo_base(a, b, n = 100, &block) | |
sum = 0 | |
res = 0 | |
# чтобы в цикле не считать этот диапазон | |
range = a..b.to_f | |
for i in 0..n | |
# ваша версия rand не работала, на вход надо либо одно число, либо Range | |
x = rand(range) | |
# debug print | |
p x | |
sum += block.call(x) | |
# debug print | |
p sum | |
# вот тут может поделиться на 0 | |
res += (b - a) * 1.0 / i * sum | |
end | |
res | |
end | |
def some_other_function(x) | |
x**2 | |
end | |
p integrating_Monte_Carlo_base(1, 2) { |x| some_other_function(x)} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment