Skip to content

Instantly share code, notes, and snippets.

@googya
Created November 9, 2018 10:43
Show Gist options
  • Save googya/7f0808db19346cbd2846f52d04bf8f05 to your computer and use it in GitHub Desktop.
Save googya/7f0808db19346cbd2846f52d04bf8f05 to your computer and use it in GitHub Desktop.
简单抢红包实现
require 'bigdecimal'
def getRandomMoney(remainSize, remainMoney)
if remainSize == 1
remainSize -= 1
return remainMoney.floor(2)
end
min = 0.01
max = remainMoney / remainSize * 2.0
money = rand * max
money = money < min ? 0.01 : money
money = money.floor(2)
# remainSize -= 1
# remainMoney -= money
money
end
n = 10
money = BigDecimal.new(100)
10.times do
t = getRandomMoney(n, money)
puts t.to_f
n -= 1
money -= t
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment