Created
November 9, 2018 10:43
-
-
Save googya/7f0808db19346cbd2846f52d04bf8f05 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
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