Java,彩票计算器

发布于 2024-09-09 09:46:57 字数 184 浏览 5 评论 0原文

好吧,所以我真的很无聊,决定制作一个彩票计算器类型的东西(是的,我知道,我很伤心!)

无论如何,我想知道是否有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

她如夕阳 2024-09-16 09:46:57

粗略估计:

49 * 48 * 47 * 46 * 45 * 44 = 10.068.347.520

这是包含所有可能组合的列表的长度。请注意,您不能使用 ArrayList,因为它由数组支持,并且数组的最大大小限制为 Integer.MAX_VALUE。即使你使用byte数组来存储百亿个组合,你也应该像这样启动jvm:(

java -Xmx250G my.little.LotteryGenerator

假设你有足够的内存)

A rough estimation:

49 * 48 * 47 * 46 * 45 * 44 = 10.068.347.520

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 to Integer.MAX_VALUE. Even if you use byte arrays to store the ten billion combinations, you should start the jvm like this:

java -Xmx250G my.little.LotteryGenerator

(assuming you have sufficient memory on board)

握住你手 2024-09-16 09:46:57

这是我的第二个答案。这是一个愚蠢但简单的方法来解决这个问题(用Java):

for (int i1=1; i1<45; i1++) {
   for (int i2=i1+1; i2<46; i2++) {
      for (int i3=i2+1; i3<47; i3++) {
         for (int i4=i3+1; i4<48; i4++) {
            for (int i5=i4+1; i5<49; i5++) {
               for (int i6=i5+1; i6<50; i6++) {
                  System.out.format("%d %d %d %d %d %d\n", i1, i2, i3, i4, i5, i6);
}}}}}}

This is my second answer. Here's a stupid but easy approach to coding this problem (in Java):

for (int i1=1; i1<45; i1++) {
   for (int i2=i1+1; i2<46; i2++) {
      for (int i3=i2+1; i3<47; i3++) {
         for (int i4=i3+1; i4<48; i4++) {
            for (int i5=i4+1; i5<49; i5++) {
               for (int i6=i5+1; i6<50; i6++) {
                  System.out.format("%d %d %d %d %d %d\n", i1, i2, i3, i4, i5, i6);
}}}}}}
糖粟与秋泊 2024-09-16 09:46:57

对于用 Java 编写自己的彩票程序时的意大利面条式代码和可怕的变量命名(我一晚熬夜到凌晨 3 点)感到抱歉,但是

 public static void LottoNumbers()
        {

          int zero=0;
 int one=0;
 int two=0;
 int three=0;
 int four=0;
 int five=0;
 int six=0;
 int bonus = 0;
 int bonusball=0;
            ArrayList myNumbers = new ArrayList();
            Random random2 = new Random();

            ArrayList ResultsList = new ArrayList<results>();
            ArrayList numberList = new ArrayList();

            for (int outer = 0; outer < 140000001; outer++)
            {
                myNumbers.clear();
                for (int i = 1; i < 7; i++)
                {
                    boolean a = true;
                    while(a)
                    {
                    int r = random2.nextInt();
                    if (!myNumbers.contains(r)) {
                        myNumbers.add(random2.nextInt(49) +1);
                        a = false;
                    }
                    }
                }
                numberList.clear();
                for (int i = 1; i < 7; i++)
                {
                    boolean a = true;
                    while (a)
                    {


                        Random random = new Random();
                        int r = random.nextInt(49) +1;
                        if (!numberList.contains(r))
                        {
                            numberList.add(r);
                            a = false;
                        }
                    }
                }
                Random random = new Random();
                boolean b = true;
                while(b)
                {
                int bb = random.nextInt(49) +1;
                if (!numberList.contains(bb))
                {
                    bonusball = bb;
                    b = false;
                }
                }

                int matches = 0;
                for (int u =0; u<numberList.size(); u++)
                {
                    if (myNumbers.contains(numberList.get(u)))
                    {
                        matches++;
                    }
                }
                if (matches == 0)
                        zero++;
                    if (matches == 1)
                        one++;
                    if (matches == 2)
                        two++;
                    if (matches == 3)
                        three++;
                    if (matches == 4)
                        four++;
                    if (matches == 5)
                        five++;
                    if (matches== 5 && myNumbers.contains(bonusball))
                        bonus++;
                    if (matches == 6) {
                        six++;

                             System.out.println("Jackpot! " + numberList.get(0)
                                     +"," + numberList.get(1)
                                     +"," + numberList.get(2)
                                     +"," + numberList.get(3)
                                     +"," + numberList.get(4)
                                     +"," + numberList.get(5));
                    }


if (outer%500000==0) {
                        System.out.println("none: " + zero + " one: " + one + " two: " + two + " three: " + three + " four: " + four +
                        " five: " + five + " 5+bonus: " + bonus + " six: " + six + " total: " + outer);
}

我最初的理论是针对我自己选择的一组 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

 public static void LottoNumbers()
        {

          int zero=0;
 int one=0;
 int two=0;
 int three=0;
 int four=0;
 int five=0;
 int six=0;
 int bonus = 0;
 int bonusball=0;
            ArrayList myNumbers = new ArrayList();
            Random random2 = new Random();

            ArrayList ResultsList = new ArrayList<results>();
            ArrayList numberList = new ArrayList();

            for (int outer = 0; outer < 140000001; outer++)
            {
                myNumbers.clear();
                for (int i = 1; i < 7; i++)
                {
                    boolean a = true;
                    while(a)
                    {
                    int r = random2.nextInt();
                    if (!myNumbers.contains(r)) {
                        myNumbers.add(random2.nextInt(49) +1);
                        a = false;
                    }
                    }
                }
                numberList.clear();
                for (int i = 1; i < 7; i++)
                {
                    boolean a = true;
                    while (a)
                    {


                        Random random = new Random();
                        int r = random.nextInt(49) +1;
                        if (!numberList.contains(r))
                        {
                            numberList.add(r);
                            a = false;
                        }
                    }
                }
                Random random = new Random();
                boolean b = true;
                while(b)
                {
                int bb = random.nextInt(49) +1;
                if (!numberList.contains(bb))
                {
                    bonusball = bb;
                    b = false;
                }
                }

                int matches = 0;
                for (int u =0; u<numberList.size(); u++)
                {
                    if (myNumbers.contains(numberList.get(u)))
                    {
                        matches++;
                    }
                }
                if (matches == 0)
                        zero++;
                    if (matches == 1)
                        one++;
                    if (matches == 2)
                        two++;
                    if (matches == 3)
                        three++;
                    if (matches == 4)
                        four++;
                    if (matches == 5)
                        five++;
                    if (matches== 5 && myNumbers.contains(bonusball))
                        bonus++;
                    if (matches == 6) {
                        six++;

                             System.out.println("Jackpot! " + numberList.get(0)
                                     +"," + numberList.get(1)
                                     +"," + numberList.get(2)
                                     +"," + numberList.get(3)
                                     +"," + numberList.get(4)
                                     +"," + numberList.get(5));
                    }


if (outer%500000==0) {
                        System.out.println("none: " + zero + " one: " + one + " two: " + two + " three: " + three + " four: " + four +
                        " five: " + five + " 5+bonus: " + bonus + " six: " + six + " total: " + outer);
}

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!

萌逼全场 2024-09-16 09:46:57

谷歌搜索“permutations java”发现这个

玩得开心!

Google on "permutations java" found this.

Have fun!

﹂绝世的画 2024-09-16 09:46:57

如果你这样做是为了好玩,你不应该从头开始吗?

富有想象力的代码还有加分吗?看起来是尝试递归的理想机会。

认为

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文