开发者

Java, lottery calculuator

开发者 https://www.devze.com 2023-01-07 03:28 出处:网络
Ok so i am really bored and have decided to make a lottery calculator type thing (yes i know, i am sad!)

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开发者_StackOverflow for me to write my own?

Thank you


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)


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);
}}}}}}


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!


Google on "permutations java" found this.

Have fun!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消