Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class lotto {
- public ArrayList<Integer> oneNumber = GetNumber();
- public ArrayList<Integer> lottoDraw = GetNumbers();
- public ArrayList<Double> howmanyTimes = new ArrayList<Double>();
- public ArrayList<Double> outcome = new ArrayList<Double>();
- public ArrayList<Double> nums = new ArrayList<Double>();
- public ArrayList<Integer> times = new ArrayList<Integer>();
- public ArrayList<String> storeLotnum = new ArrayList<String>();
- //Creating multidimensional arraylist. {{0},{1},{2},{3},{4},{5},{6},{7},{8}}
- public ArrayList<ArrayList<String>> all = new ArrayList<ArrayList<String>>();
- //Draw one bonus number.
- public static ArrayList<Integer> GetNumber() {
- ArrayList<Integer> bonusNumber = new ArrayList<Integer>();
- //We only need one random bonusnumber.
- bonusNumber.add((int) ((40 * Math.random()) + 1));
- return bonusNumber;
- }
- //Get lottotickets and draw one "correct" lottonumber.
- public static ArrayList<Integer> GetNumbers() {
- ArrayList<Integer> allNum = new ArrayList<Integer>();
- //First adding all 40 numbers.
- for (int i = 1; i < 41; i++) {
- allNum.add(i);
- }
- //Shuffle because we dont wanna dublicates.
- Collections.shuffle(allNum);
- ArrayList<Integer> lottoNum = new ArrayList<Integer>();
- //Adding 7 numbers to list.
- for (int i = 0; i < 7; i++) {
- lottoNum.add(allNum.get(i));
- }
- //Sorting lottonum from smaller to bigger number.
- Collections.sort(lottoNum);
- return lottoNum;
- }
- //Prints the correct lottonumbers.
- public void ShowCorrectlottonum(){
- System.out.print("\n");
- System.out.println("Lottery correct numbers: " + lottoDraw + " + " + "[" + oneNumber.get(0) + "]");
- System.out.print("\n");
- }
- public void PlayLotto(double num){
- //Filling nums list.
- for(int i = 0; i < 9; i++) {
- nums.add(0.0);
- }
- //Adding 9 rows to "all" list.
- for(int i = 0; i < 9; i++) {
- all.add(new ArrayList<String>());
- }
- //Looping all played tickets.
- for (int i = 0; i < num; i++) {
- ArrayList<Integer> lottoNum2 = GetNumbers();
- ArrayList<Integer> storage = new ArrayList<Integer>(lottoDraw); //Put the correct lottonumbers to storage, so you can check how many numbers are correct.
- //Lottonumber to string.
- String listString = lottoNum2.toString();
- listString = listString.substring(1, listString.length()-1);
- storeLotnum.add(listString);
- //Removing elements from storage that are not contained in the lottonum2.
- storage.retainAll(lottoNum2);
- int idx = storage.size();
- double value = nums.get(idx);
- //Checking storage size = how many numbers are correct, adding one into variable and then adding lottoNum to string list.
- if(idx == 6 && lottoNum2.contains(oneNumber.get(0))) {
- nums.set(8, (nums.get(8) + 1));
- all.get(8).add(listString);
- }
- else if(idx >= 4) {
- nums.set(idx,value + 1);
- all.get(idx).add(listString);
- }
- else {
- value = nums.get(0) + 1;
- nums.set(0,value);
- }
- }
- //Adding variables,counting probability and converting to int.
- for(int i = 0; i < nums.size(); i++) {
- howmanyTimes.add(i, nums.get(i));
- outcome.add(i, (howmanyTimes.get(i)/num)*100);
- times.add(howmanyTimes.get(i).intValue());
- }
- }
- //Prints all played tickets.
- public void ShowTickets(){
- for(int i = 0; i < storeLotnum.size(); i++) {
- System.out.println("\n" + storeLotnum.get(i) + "\n");
- }
- }
- //Prints all winningnumbers.
- public void ShowWinningnumbers(){
- //Looping backwords.
- for(int i = 7; i > 3; i--) {
- if(i > 3) this.PrintTicket(i);
- if(i == 7) this.PrintTicket(8);
- }
- }
- private void PrintTicket(int idx){
- String[] correctNum = {"0","1","2","3","4","5","6","7","6+1"};
- System.out.printf("\n|All %s correct numbers|\n", correctNum[idx]);
- //Looping list and print.
- for(int j = 0; j < all.get(idx).size(); j++) {
- System.out.println(all.get(idx).get(j) + "\n");
- }
- }
- //Prints how many pcs. in every categories.
- public void ShowAllwinning(){
- System.out.println("|The number of winning categories|");
- System.out.printf("7 correct: \t\t%d pcs.\n6+1 correct: \t\t%d pcs.\n6 correct: \t\t%d pcs.\n5 correct: \t\t%d pcs.\n4 correct: \t\t%d pcs.\nTickets with no win: \t%d pcs.\n",times.get(7),times.get(8),times.get(6),times.get(5),times.get(4),times.get(0));
- System.out.print("\n");
- }
- //Prints probabilities.
- public void ShowOdds(){
- System.out.println("|Probabilities|");
- System.out.printf("7 correct: \t\t%.10f%%\n6+1 correct: \t\t%.10f%%\n6 correct: \t\t%.10f%%\n5 correct: \t\t%.10f%%\n4 correct: \t\t%.10f%%\n", outcome.get(7),outcome.get(8),outcome.get(6),outcome.get(5),outcome.get(4));
- }
- }
- //PÄÄOHJELMA.
- public class javaykskolme {
- public static void main(String[] args) {
- lotto newlotto = new lotto();
- newlotto.PlayLotto(18000000);
- //newlotto.ShowTickets();
- newlotto.ShowCorrectlottonum();
- newlotto.ShowWinningnumbers();
- newlotto.ShowCorrectlottonum();
- newlotto.ShowAllwinning();
- newlotto.ShowOdds();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement