Java,彩票计算器
好吧,所以我真的很无聊,决定制作一个彩票计算器类型的东西(是的,我知道,我很伤心!)
无论如何,我想知道是否有一个 java 库方法/类来计算排列/组合。我想生成所有可能的数字集,其中有 1 - 49 之间的 6 个数字,其中没有重复。
如果这不能用作预先编写的方法,那么我自己编写的最佳方法是什么?
谢谢
Ok so i am really bored and have decided to make a lottery calculator type thing (yes i know, i am sad!)
Anyway, i was wondering if there was a java library method/class for working out permutations/combinations. I want to generate all possible number sets, that have 6 numbers from 1 - 49 with no repeats in them
If this isnt available as a pre written method, whats the best approach for me to write my own?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
粗略估计:
这是包含所有可能组合的列表的长度。请注意,您不能使用
ArrayList
,因为它由数组支持,并且数组的最大大小限制为Integer.MAX_VALUE
。即使你使用byte
数组来存储百亿个组合,你也应该像这样启动jvm:(假设你有足够的内存)
A rough estimation:
This is the length of a list containing all possible combinations. Note that you can't use an
ArrayList
because this is backed by an array and an arrays maximum size is limited toInteger.MAX_VALUE
. Even if you usebyte
arrays to store the ten billion combinations, you should start the jvm like this:(assuming you have sufficient memory on board)
这是我的第二个答案。这是一个愚蠢但简单的方法来解决这个问题(用Java):
This is my second answer. Here's a stupid but easy approach to coding this problem (in Java):
对于用 Java 编写自己的彩票程序时的意大利面条式代码和可怕的变量命名(我一晚熬夜到凌晨 3 点)感到抱歉,但是
我最初的理论是针对我自己选择的一组 6 个数字运行 6 个随机数。但我发现随机数生成器有点不可靠,1、2、3、4、5、6 会赢得大约一百万次,有时甚至更多!但随后我决定也随机生成我的号码,我发现结果相当准确,至少大约每 1400 万次迭代就会出现一组获胜(基于英国)的 6 个匹配号码。
我对每个组合应该在 1400 万次出现一次的想法很感兴趣,并且想要进行 1.4 亿次抽奖,看看哪些数字出现最多。当我意识到存储所有数据会多么令人头痛时,我放弃了。所以我只是插入一些打印行来输出总数(即 5 场比赛)和发生的任何累积奖金。这个周末可能会帮我选几个号码!
在任何人开枪之前,我是一个新的、热情的程序员,我知道这有点乱,但这只是为了好玩:)
编辑:程序刚刚完成第 1.4 亿次循环,有 10 个大奖!
Sorry for the spaghetti code and horrible naming of variables (I stayed up till 3AM one night) writing my own lottery program in Java, but here ya go
My initial theory was to run 6 random numbers against a set of 6 numbers chosen by myself. But I found the random number generator to be a little unreliable, 1, 2, 3, 4, 5, 6 would win around once a million, sometimes more! But I then decided to also generate my numbers randomly and the results I found were pretty accurate, at least in that a winning (UK based) set of 6 matching numbers appeared roughly once every 14 million iterations.
I was intrigued by the idea that every combination should appear once in 14million, and wanted to do 140million draws and see which numbers came up most. I gave up when I realised what a headache it would be storing all that data. So I just stuck in some print lines to output totals (ie. 5 matches) and any jackpots that occurred. Might help me pick a few numbers this weekend!
Before anyone fires any shots, I'm a new and enthusiastic programmer and know that this is a bit of a mess, but its just for fun :)
Edit: the program just finished its 140millionth loop, and there were 10 jackpots!
谷歌搜索“permutations java”发现这个。
玩得开心!
Google on "permutations java" found this.
Have fun!
如果你这样做是为了好玩,你不应该从头开始吗?
富有想象力的代码还有加分吗?看起来是尝试递归的理想机会。
认为
getPermissions(length)
{
结果=新向量
tmp = getPermitations(lenght-1)
for (i = 0=>9)
{
for (字符串 s: tmp)
{
结果.add(i + tmp);
}
是
的
,我知道这不是工作代码,复制别人工作代码的乐趣在哪里?
然后通过优化它来获得额外的分数。
If you are doing this for fun, should you not be doing it from scratch?
With extra points for imaginative code? Looks like an ideal chance to try out recursion.
Think
getPermitations(length)
{
result = new Vector
tmp = getPermitations(lenght-1)
for (i = 0=>9)
{
for (String s: tmp)
{
result.add(i + tmp);
}
}
}
Yes, I know that is not working code, where is the fun in copying someone else working code?
Then play with optimising it for extra points.