// Like a quickcheck // To generate random 7, we need at least call random5() twice. // All 25 possibilities. // A easy solution is only taking results from 1 - 7. // But to reduce a lot retries, we can take results from 1 - 21. And divide number by 3. static int MAX_TRY = 1000; for (int i = 0 ; i < MAX_TRY ; i ++) { int raw = (rand5() - 1 ) * 5 + rand5(); if (raw > 21) continue; return ((raw - 1) / 7) + 1; } throw Exception("Failed to generate");
CC150 19.10
原创furuijie8679 博主文章分类:Interview ©著作权
©著作权归作者所有:来自51CTO博客作者furuijie8679的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:CC150 19.8
下一篇:CC150 19.11
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章