Add Monte Carlo
This commit is contained in:
		
							parent
							
								
									b4c14ad648
								
							
						
					
					
						commit
						5fb49043fa
					
				
					 5 changed files with 276 additions and 0 deletions
				
			
		
							
								
								
									
										97
									
								
								Vorbereitungen/Konto.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										97
									
								
								Vorbereitungen/Konto.java
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,97 @@
 | 
				
			||||||
 | 
					import java.util.Scanner;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public class Konto {
 | 
				
			||||||
 | 
					    static double kontostand;
 | 
				
			||||||
 | 
					    static Scanner scanner;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private static boolean isInt(String input) {
 | 
				
			||||||
 | 
					        try {
 | 
				
			||||||
 | 
					            Integer.parseInt(input);
 | 
				
			||||||
 | 
					        } catch (NumberFormatException nfe) {
 | 
				
			||||||
 | 
					            return false;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        return true;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private static void showBalance() {
 | 
				
			||||||
 | 
					        System.out.printf("Kontostand: %f", kontostand);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private static void deposit() {
 | 
				
			||||||
 | 
					        System.out.printf("Bitte geben Sie an, wie viel Geld sie einzahlen möchten: ");
 | 
				
			||||||
 | 
					        String userInput;
 | 
				
			||||||
 | 
					        while (true) {
 | 
				
			||||||
 | 
					            userInput = scanner.nextLine();
 | 
				
			||||||
 | 
					            if (isInt(userInput)) {
 | 
				
			||||||
 | 
					                Double.parseDouble(userInput);
 | 
				
			||||||
 | 
					                break;
 | 
				
			||||||
 | 
					            } else {
 | 
				
			||||||
 | 
					                System.out.println("Bitte geben Sie eine ganze Zahl ein.");
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        double cleanUserInput = Double.parseDouble(userInput);
 | 
				
			||||||
 | 
					        kontostand += cleanUserInput;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private static void withdraw() {
 | 
				
			||||||
 | 
					        System.out.printf("Bitte geben Sie ein, wie viel Sie abheben möchten: ");
 | 
				
			||||||
 | 
					        String userInput;
 | 
				
			||||||
 | 
					        while (true) {
 | 
				
			||||||
 | 
					            userInput = scanner.nextLine();
 | 
				
			||||||
 | 
					            if (isInt(userInput)) {
 | 
				
			||||||
 | 
					                Double.parseDouble(userInput);
 | 
				
			||||||
 | 
					                break;
 | 
				
			||||||
 | 
					            } else {
 | 
				
			||||||
 | 
					                System.out.println("Bitte geben Sie eine ganze Zahl ein.");
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            double cleanUserInput = Double.parseDouble(userInput);
 | 
				
			||||||
 | 
					            if (cleanUserInput > kontostand) {
 | 
				
			||||||
 | 
					                System.out.println("Der eingegebene Betrag ist größer als der Kontostand.");
 | 
				
			||||||
 | 
					                return;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            kontostand -= cleanUserInput;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private static void menu() {
 | 
				
			||||||
 | 
					        System.out.println("1. Kontostandanzeige");
 | 
				
			||||||
 | 
					        System.out.println("2. Einzahlung");
 | 
				
			||||||
 | 
					        System.out.println("3. Auszahlung");
 | 
				
			||||||
 | 
					        System.out.println("4. Verlassen");
 | 
				
			||||||
 | 
					        System.out.printf("Eingabe: ");
 | 
				
			||||||
 | 
					        String userInput;
 | 
				
			||||||
 | 
					        while (true) {
 | 
				
			||||||
 | 
					            userInput = scanner.nextLine();
 | 
				
			||||||
 | 
					            if (isInt(userInput)) {
 | 
				
			||||||
 | 
					                Integer.parseInt(userInput);
 | 
				
			||||||
 | 
					                break;
 | 
				
			||||||
 | 
					            } else {
 | 
				
			||||||
 | 
					                System.out.println("Bitte geben Sie eine ganze Zahl ein.");
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        int cleanUserInput = Integer.parseInt(userInput);
 | 
				
			||||||
 | 
					        while (true) {
 | 
				
			||||||
 | 
					            switch (cleanUserInput) {
 | 
				
			||||||
 | 
					                case 1:
 | 
				
			||||||
 | 
					                    showBalance();
 | 
				
			||||||
 | 
					                    break;
 | 
				
			||||||
 | 
					                case 2:
 | 
				
			||||||
 | 
					                    deposit();
 | 
				
			||||||
 | 
					                    break;
 | 
				
			||||||
 | 
					                case 3:
 | 
				
			||||||
 | 
					                    withdraw();
 | 
				
			||||||
 | 
					                    break;
 | 
				
			||||||
 | 
					                case 4:
 | 
				
			||||||
 | 
					                    System.out.println("Verlassen...");
 | 
				
			||||||
 | 
					                    System.exit(0);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public static void main(String[] args) {
 | 
				
			||||||
 | 
					        kontostand = 0;
 | 
				
			||||||
 | 
					        scanner = new Scanner(System.in);
 | 
				
			||||||
 | 
					        menu();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										2
									
								
								Vorbereitungen/MonteCarlo/manifest.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								Vorbereitungen/MonteCarlo/manifest.txt
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,2 @@
 | 
				
			||||||
 | 
					Main-Class: vorbereitungen.MonteCarlo.MonteCarlo
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,52 @@
 | 
				
			||||||
 | 
					package vorbereitungen.MonteCarlo;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.Scanner;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public class MonteCarlo {
 | 
				
			||||||
 | 
					    public static void main(String args[]) {
 | 
				
			||||||
 | 
					        Scanner scanner = new Scanner(System.in);
 | 
				
			||||||
 | 
					        System.out.println("Enter how many attempts you want to run:");
 | 
				
			||||||
 | 
					        String attempts = scanner.nextLine();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        try {
 | 
				
			||||||
 | 
					            int num_attempts = Integer.parseInt(attempts);
 | 
				
			||||||
 | 
					        } catch(NumberFormatException nfe) {
 | 
				
			||||||
 | 
					            System.out.println("Invalid input.");
 | 
				
			||||||
 | 
					            scanner.close();
 | 
				
			||||||
 | 
					            System.exit(1);
 | 
				
			||||||
 | 
					            return;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        int num_attempts = Integer.parseInt(attempts);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        System.out.println("Enter how large the initial bankroll should be:");
 | 
				
			||||||
 | 
					        String bankroll = scanner.nextLine();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        try {
 | 
				
			||||||
 | 
					            int num_bankroll = Integer.parseInt(bankroll);
 | 
				
			||||||
 | 
					        } catch(NumberFormatException nfe) {
 | 
				
			||||||
 | 
					            System.out.println("Invalid input.");
 | 
				
			||||||
 | 
					            scanner.close();
 | 
				
			||||||
 | 
					            System.exit(1);
 | 
				
			||||||
 | 
					            return;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        int num_bankroll = Integer.parseInt(bankroll);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        System.out.println("Enter the Betsize you want to use for this simulation:");
 | 
				
			||||||
 | 
					        String betsize = scanner.nextLine();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        try {
 | 
				
			||||||
 | 
					            int num_betsize = Integer.parseInt(betsize);
 | 
				
			||||||
 | 
					        } catch(NumberFormatException nfe) {
 | 
				
			||||||
 | 
					            System.out.println("Invalid input.");
 | 
				
			||||||
 | 
					            scanner.close();
 | 
				
			||||||
 | 
					            System.exit(1);
 | 
				
			||||||
 | 
					            return;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        int num_betsize = Integer.parseInt(betsize);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        scanner.close();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        Simulation simulation = new Simulation(num_attempts, num_bankroll, num_betsize);
 | 
				
			||||||
 | 
					        simulation.run();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,67 @@
 | 
				
			||||||
 | 
					package vorbereitungen.MonteCarlo;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.concurrent.ThreadLocalRandom;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public class Simulation{
 | 
				
			||||||
 | 
					    int max_attempts;
 | 
				
			||||||
 | 
					    int current_attempts;
 | 
				
			||||||
 | 
					    double starting_bankroll;
 | 
				
			||||||
 | 
					    double current_bankroll;
 | 
				
			||||||
 | 
					    double simulated_betsize;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public Simulation(int max_atts, int bankroll, double betsize) {
 | 
				
			||||||
 | 
					         this.max_attempts = max_atts;
 | 
				
			||||||
 | 
					         this.current_attempts = 1;
 | 
				
			||||||
 | 
					         this.starting_bankroll = bankroll;
 | 
				
			||||||
 | 
					         this.current_bankroll = bankroll;
 | 
				
			||||||
 | 
					         this.simulated_betsize = betsize;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public static int roll() {
 | 
				
			||||||
 | 
					        return ThreadLocalRandom.current().nextInt(1, 8);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public static double calculate(int player_roll, int dealer_roll) {
 | 
				
			||||||
 | 
					        int diff = dealer_roll - player_roll;
 | 
				
			||||||
 | 
					        if (diff >= 1) {return 0;}
 | 
				
			||||||
 | 
					        else if (diff == 0) {return 1;}
 | 
				
			||||||
 | 
					        else if (diff == -6) {return 2;}
 | 
				
			||||||
 | 
					        else {return 1.5;}
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public void handle() {
 | 
				
			||||||
 | 
					        if (this.simulated_betsize > this.current_bankroll) {
 | 
				
			||||||
 | 
					            System.out.println("|===Out of money===|");
 | 
				
			||||||
 | 
					            System.out.printf("|Attempts: %d      |\n", this.current_attempts);
 | 
				
			||||||
 | 
					            System.out.println("===================|");
 | 
				
			||||||
 | 
					            System.exit(2137);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        this.current_bankroll -= this.simulated_betsize;
 | 
				
			||||||
 | 
					        int player_roll = roll();
 | 
				
			||||||
 | 
					        int house_roll = roll();
 | 
				
			||||||
 | 
					        double mult = calculate(player_roll, house_roll);
 | 
				
			||||||
 | 
					        this.current_bankroll += this.simulated_betsize*mult;
 | 
				
			||||||
 | 
					        this.current_attempts++;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public void print_progress() {
 | 
				
			||||||
 | 
					        double bankroll_percentage = (this.current_bankroll/this.starting_bankroll)*100;
 | 
				
			||||||
 | 
					        double attempt_percentage = (this.current_attempts/this.max_attempts)*100;
 | 
				
			||||||
 | 
					        System.out.printf("\033[2J"); // Clear screen
 | 
				
			||||||
 | 
					        System.out.println("|===========|");
 | 
				
			||||||
 | 
					        System.out.printf("|Current Attempt: %d/%d|\n", this.current_attempts, this.max_attempts);
 | 
				
			||||||
 | 
					        System.out.printf("|Current Bankroll: %f|\n", this.current_bankroll);
 | 
				
			||||||
 | 
					        System.out.printf("|%f%% left of starting bankroll\n|", bankroll_percentage);
 | 
				
			||||||
 | 
					        System.out.printf("|%f%% done|", attempt_percentage);
 | 
				
			||||||
 | 
					        System.out.println("|===========|");
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public void run(){
 | 
				
			||||||
 | 
					        for (int i = 0; i < max_attempts; i++) {
 | 
				
			||||||
 | 
					            handle();
 | 
				
			||||||
 | 
					            if (this.current_attempts % 7500 == 0 || this.current_attempts == 1) {
 | 
				
			||||||
 | 
					                print_progress();
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										58
									
								
								Vorbereitungen/SieveOfErathostenes.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										58
									
								
								Vorbereitungen/SieveOfErathostenes.java
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,58 @@
 | 
				
			||||||
 | 
					import java.util.Arrays;
 | 
				
			||||||
 | 
					import java.util.Scanner;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public class SieveOfErathostenes {
 | 
				
			||||||
 | 
					    private static void isValid(String input) {
 | 
				
			||||||
 | 
					        if (input == null) {System.exit(-1);}
 | 
				
			||||||
 | 
					        try {
 | 
				
			||||||
 | 
					            Integer.parseInt(input);
 | 
				
			||||||
 | 
					        } catch (NumberFormatException nfe) {
 | 
				
			||||||
 | 
					            System.out.println("Die eingegebene Zahl ist keine ganze Zahl");
 | 
				
			||||||
 | 
					            System.exit(-1);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        int cleanInput = Integer.parseInt(input);
 | 
				
			||||||
 | 
					        if (cleanInput < 2 || cleanInput > 1000) {
 | 
				
			||||||
 | 
					            System.out.println("Die eingegebene Zahl muss mindestens 2 sein und höchstens 1000 sein.");
 | 
				
			||||||
 | 
					            System.exit(-1);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private static void printArray(boolean[] arr) {
 | 
				
			||||||
 | 
					        for (int i = 0; i < arr.length; i++) {
 | 
				
			||||||
 | 
					            if (arr[i]) {
 | 
				
			||||||
 | 
					                System.out.printf("%d ", i + 2);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        System.out.printf("%n");
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public static void main(String[] args) {
 | 
				
			||||||
 | 
					        //System.out.println("Sanity Check");
 | 
				
			||||||
 | 
					        Scanner input_scanner = new Scanner(System.in);
 | 
				
			||||||
 | 
					        System.out.println("Bitte gib eine Zahl ein. ");
 | 
				
			||||||
 | 
					        String user_input = input_scanner.nextLine();
 | 
				
			||||||
 | 
					        input_scanner.close();
 | 
				
			||||||
 | 
					        isValid(user_input);
 | 
				
			||||||
 | 
					        int clean_input = Integer.parseInt(user_input);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        boolean[] primes = new boolean[clean_input];
 | 
				
			||||||
 | 
					        Arrays.fill(primes, true);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        for (int i = 1; i < clean_input; i++) {
 | 
				
			||||||
 | 
					            if (i == 1) {continue;}
 | 
				
			||||||
 | 
					            if (primes[i]) {
 | 
				
			||||||
 | 
					                for (int j = 2; j < clean_input; j++) {
 | 
				
			||||||
 | 
					                    int tmp = j*i;
 | 
				
			||||||
 | 
					                    if (tmp > clean_input) {
 | 
				
			||||||
 | 
					                        break;
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                    primes[tmp - 2] = false;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        primes[clean_input - 1] = false;
 | 
				
			||||||
 | 
					        printArray(primes);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
		Reference in a new issue