每次隨機(jī)金額都一樣! 問(wèn)題是不是理解錯(cuò)了。
后面老師說(shuō)了?。?!問(wèn)題作廢 //?double_average_test.go package?algo import?( ???"fmt" ???"testing" ) func?TestDoubleAverage(t?*testing.T)?{ ???sum?:=?int64(0) ???count,?amount?:=?int64(10),?int64(100*100) ???for?i?:=?int64(0);?i?<?count;?i++?{ ??????x?:=?DoubleAverage(count?-?i,?amount)?//每次人減少 ??????amount?-=?x????????????????????????//每次紅包減少 ??????fmt.Println(x) ??????sum?+=?x ???} ???fmt.Println("--總計(jì)結(jié)束--") ???fmt.Println(sum) }
代碼
//double_average.go package?algo import?( ???"math/rand" ???"time" ) func?init()?{ ???rand.Seed(time.Now().UnixNano())?//這里保證啟動(dòng)的時(shí)候隨機(jī) } const?min?=?int64(1) func?DoubleAverage(count,?amount?int64)?int64?{ ???if?count?==?1?{ ??????return?amount ???} ???//?計(jì)算出最大可用金額 ???max?:=?amount?-?min*count ???//?計(jì)算最大可用的平均值 ???avg?:=?max?/?count ???//?計(jì)算2倍均值 ???avg2?:=?avg?*?2?+?min ???//?隨機(jī)紅包序列,?把二倍均值作為隨機(jī)數(shù)的最大數(shù) ???x?:=?rand.Int63n(avg2)?+?min ???return?x }