Skip to content

Instantly share code, notes, and snippets.

@Wolwer1nE
Created October 27, 2019 18:47
Show Gist options
  • Save Wolwer1nE/c9423f662f39c39dd1952587334cebbd to your computer and use it in GitHub Desktop.
Save Wolwer1nE/c9423f662f39c39dd1952587334cebbd to your computer and use it in GitHub Desktop.
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