private decimal GetRandomMoney(RedPocket redPackage, decimal min, int n) { // RemainCount 剩余的红包数量 // Balance 剩余的钱 if (redPackage.RemainCount <= 0) { return(0); } if (redPackage.RemainCount == 1) { redPackage.RemainCount--; return(Math.Round(redPackage.Balance * n) / n); } Random r = new Random(); decimal max = redPackage.Balance / redPackage.RemainCount * 2; decimal money = (decimal)r.NextDouble() * max; money = money <= min ? min : money; money = Math.Floor(money * n) / n; redPackage.RemainCount--; redPackage.Balance -= money; return(money); }
public void TestMethod1() { const int count = 10; var redPocket = new RedPocket() { Balance = 1000.12345678M, RemainCount = count }; for (int i = 0; i < count; i++) { var v = GetRandomMoney(redPocket, 0.00000001M, 100000000); System.Diagnostics.Debug.Print(v.ToString()); } }