najimCseJu

randX using randY

Sep 27th, 2017
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.57 KB | None | 0 0
  1. import java.io.*;
  2. import java.lang.reflect.Array;
  3. import java.nio.charset.StandardCharsets;
  4. import java.nio.file.StandardOpenOption;
  5. import java.util.Arrays;
  6. import java.util.Collections;
  7. import java.util.Random;
  8. import java.util.StringTokenizer;
  9.  
  10.  
  11. public class Main {
  12.     static Random rand = new Random();
  13.  
  14.  
  15.     static int rand3()
  16.     {
  17.         return rand.nextInt(3);
  18.         //return (int) (Math.random()*3);
  19.     }
  20.     static int rand27()
  21.     {
  22.         return (3*3*rand3()) + (3*rand3())  + rand3();
  23.     }
  24.     static int rand11()
  25.     {
  26.         int r = rand27()/2;
  27.         if(r>10)
  28.         {
  29.             return rand11();
  30.         }
  31.         return r;
  32.     }
  33.     static int betterApproach()
  34.     {
  35.         return rand11();
  36.     }
  37.     static int centrallyBiased()
  38.     {
  39.         return (rand3() + rand3() + rand3() + rand3() + rand3() + rand3() )%11;
  40.     }
  41.     static int roundOffError1()
  42.     {
  43.         return (6*rand3())%11;
  44.     }
  45.     static int roundOffError2()
  46.     {
  47.         return (int) ((6*rand3())/11.0);
  48.     }
  49.     static int roundOffError3()
  50.     {
  51.         return (int) ((rand3()/2.0) * 10);
  52.     }
  53.     public static void main(String[] args) throws IOException {
  54.  
  55.        Reader reader = new Reader().InitConsoleReading();
  56.         //Reader reader = new Reader().InitFileReading("C:\\Users\\najim\\OneDrive\\Documents\\in_large.txt");
  57.         Writer writer = new Writer().InitConsoleWriting();
  58.         //Writer writer = new Writer().InitFileWriting("C:\\Users\\najim\\OneDrive\\Documents\\out_java.txt");
  59.  
  60.         int frequencyA[] = new int[11];Arrays.fill(frequencyA,0);
  61.         int frequencyB[] = new int[11];Arrays.fill(frequencyB,0);
  62.         int frequencyC[] = new int[11];Arrays.fill(frequencyC,0);
  63.         int frequencyD[] = new int[11];Arrays.fill(frequencyD,0);
  64.         int frequencyE[] = new int[11];Arrays.fill(frequencyE,0);
  65.         int iteration = 100000;
  66.         for(int i = 0;i<100000;i++)
  67.         {
  68.             int a = betterApproach();frequencyA[a]++;
  69.             int b = centrallyBiased();frequencyB[b]++;
  70.             int c = roundOffError1();frequencyC[c]++;
  71.             int d = roundOffError2();frequencyD[d]++;
  72.             int e = roundOffError3();frequencyE[e]++;
  73.         }
  74.         for(int i=0;i<11;i++) System.out.printf("%10d",i);
  75.         System.out.println("");
  76.         for(int i=0;i<11;i++) System.out.printf("%9.2f%%",(frequencyA[i]*100.0)/iteration);
  77.         System.out.println("");
  78.         for(int i=0;i<11;i++) System.out.printf("%9.2f%%",((double)frequencyB[i]*100.0)/iteration);
  79.         System.out.println("");
  80.         for(int i=0;i<11;i++) System.out.printf("%9.2f%%",((double)frequencyC[i]*100.0)/iteration);
  81.         System.out.println("");
  82.         for(int i=0;i<11;i++) System.out.printf("%9.2f%%",((double)frequencyD[i]*100.0)/iteration);
  83.         System.out.println("");
  84.         for(int i=0;i<11;i++) System.out.printf("%9.2f%%",((double)frequencyE[i]*100.0)/iteration);
  85.     }
  86.  
  87.     static class Reader {
  88.         public BufferedReader reader;
  89.         public StringTokenizer tokenizer;
  90.  
  91.         public Reader() {
  92.         }
  93.         public Reader InitConsoleReading()
  94.         {
  95.             reader = new BufferedReader(new InputStreamReader(System.in), 32768);
  96.             tokenizer = null;
  97.             return this;
  98.         }
  99.         public Reader InitFileReading(String filePath) throws FileNotFoundException {
  100.             reader = new BufferedReader(new FileReader(filePath), 32768);
  101.             tokenizer = null;
  102.             return this;
  103.         }
  104.         public String next() {
  105.             while (tokenizer == null || !tokenizer.hasMoreTokens()) {
  106.                 try {
  107.                     tokenizer = new StringTokenizer(reader.readLine());
  108.                 } catch (IOException e) {
  109.                     throw new RuntimeException(e);
  110.                 }
  111.             }
  112.             return tokenizer.nextToken();
  113.         }
  114.  
  115.         public int nextInt() {
  116.             return Integer.parseInt(next());
  117.         }
  118.  
  119.         public long nextLong() {
  120.             return Long.parseLong(next());
  121.         }
  122.     }
  123.     static class Writer {
  124.         private BufferedWriter log;
  125.  
  126.         public Writer InitConsoleWriting()
  127.         {
  128.             log = new BufferedWriter(new OutputStreamWriter(System.out));
  129.             return this;
  130.         }
  131.         public Writer InitFileWriting(String filePath) throws IOException {
  132.             log = new BufferedWriter(new FileWriter(filePath));
  133.             return this;
  134.         }
  135.  
  136.         public Writer WriteInt(int inp) throws IOException {
  137.             this.log.write(String.valueOf(inp));
  138.             return this;
  139.         }
  140.  
  141.         public Writer WriteChar(char inp) throws IOException {
  142.             this.log.write(inp);
  143.             return this;
  144.         }
  145.         public Writer WriteNewLine() throws IOException {
  146.             return this.WriteChar('\n');
  147.         }
  148.         public Writer WriteStr(String str) throws IOException {
  149.             this.log.write(str);
  150.             return this;
  151.         }
  152.  
  153.         public Writer WriteIntArray(int inp[], char delimChar) throws IOException {
  154.             int len = inp.length;
  155.             if(len>0)
  156.             {
  157.                 log.write(String.valueOf(inp[0]));
  158.                 for(int i=1;i<len;i++)
  159.                 {
  160.                     log.write(String.format("%c%s",delimChar,String.valueOf(inp[i])));
  161.                 }
  162.             }
  163.             return this;
  164.         }
  165.         public Writer WriteCharArray(char inp[], char delimChar) throws IOException {
  166.             int len = inp.length;
  167.             if(len>0)
  168.             {
  169.                 log.write(String.valueOf(inp[0]));
  170.                 for(int i=1;i<len;i++)
  171.                 {
  172.                     log.write(String.format("%c%s",delimChar,String.valueOf(inp[i])));
  173.                 }
  174.             }
  175.             return this;
  176.         }
  177.         //
  178.  
  179.         public Writer WriteStrArray(String inp[], char delimChar) throws IOException {
  180.             int len = inp.length;
  181.             if(len>0)
  182.             {
  183.                 log.write(inp[0]);
  184.                 for(int i=1;i<len;i++)
  185.                 {
  186.                     log.write(String.format("%c%s",delimChar,inp[i]));
  187.                 }
  188.             }
  189.             return this;
  190.         }
  191.         public Writer WriteCase(String prefix,int caseNo,String suffix) throws IOException {
  192.             return this.WriteStr(String.format("%s%d%s",prefix,caseNo,suffix));
  193.         }
  194.  
  195.         public Writer Dispose() throws IOException {
  196.             this.log.flush();
  197.             this.log.close();
  198.             return this;
  199.         }
  200.     }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment