Advertisement
totobac

Untitled

Mar 27th, 2022
1,026
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.79 KB | None | 0 0
  1. public class Main {
  2.  
  3.     public static void main(String[] args) {
  4.         int countOnes = 0;
  5.         int countTwos = 0;
  6.         int countThrees = 0;
  7.         for (int i = 0; i < 10_000; i++) {
  8.             switch(drawRandomNumber()) {
  9.                 case 1:
  10.                     countOnes++;
  11.                     break;
  12.                 case 2:
  13.                     countTwos++;
  14.                     break;
  15.                 case 3:
  16.                     countThrees++;
  17.             }
  18.         }
  19.  
  20.         NumberFormat nf = NumberFormat.getPercentInstance(Locale.ENGLISH);
  21.  
  22.  
  23.         System.out.println("TEST_1 (10_000 loops):");
  24.         System.out.println("1 returned: " + nf.format(countOnes/10_000.0));
  25.         System.out.println("2 returned: " +nf.format(countTwos/10_000.0));
  26.         System.out.println("3 returned: " +nf.format(countThrees/10_000.0));
  27.  
  28.         countOnes = 0;
  29.         countTwos = 0;
  30.         countThrees = 0;
  31.  
  32.         for (int i = 0; i < 60_000; i++) {
  33.             switch(drawRandomNumber()) {
  34.                 case 1:
  35.                     countOnes++;
  36.                     break;
  37.                 case 2:
  38.                     countTwos++;
  39.                     break;
  40.                 case 3:
  41.                     countThrees++;
  42.             }
  43.         }
  44.  
  45.  
  46.         System.out.println("TEST_2 (60_000 loops):");
  47.         System.out.println("1 returned: " + nf.format(countOnes/60_000.0));
  48.         System.out.println("2 returned: " +nf.format(countTwos/60_000.0));
  49.         System.out.println("3 returned: " +nf.format(countThrees/60_000.0));
  50.  
  51.     }
  52.  
  53.     public static int drawRandomNumber(){
  54.         double x = (Math.random()*((100 - 0)+1))+0;
  55.         if (x < 20)
  56.             return 1;
  57.         else if (x < 50)
  58.             return 2;
  59.         else
  60.             return 3;
  61.     }
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement