Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.lang.reflect.Array;
- import java.nio.charset.StandardCharsets;
- import java.nio.file.StandardOpenOption;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.Random;
- import java.util.StringTokenizer;
- public class Main {
- static Random rand = new Random();
- static int rand3()
- {
- return rand.nextInt(3);
- //return (int) (Math.random()*3);
- }
- static int rand27()
- {
- return (3*3*rand3()) + (3*rand3()) + rand3();
- }
- static int rand11()
- {
- int r = rand27()/2;
- if(r>10)
- {
- return rand11();
- }
- return r;
- }
- static int betterApproach()
- {
- return rand11();
- }
- static int centrallyBiased()
- {
- return (rand3() + rand3() + rand3() + rand3() + rand3() + rand3() )%11;
- }
- static int roundOffError1()
- {
- return (6*rand3())%11;
- }
- static int roundOffError2()
- {
- return (int) ((6*rand3())/11.0);
- }
- static int roundOffError3()
- {
- return (int) ((rand3()/2.0) * 10);
- }
- public static void main(String[] args) throws IOException {
- Reader reader = new Reader().InitConsoleReading();
- //Reader reader = new Reader().InitFileReading("C:\\Users\\najim\\OneDrive\\Documents\\in_large.txt");
- Writer writer = new Writer().InitConsoleWriting();
- //Writer writer = new Writer().InitFileWriting("C:\\Users\\najim\\OneDrive\\Documents\\out_java.txt");
- int frequencyA[] = new int[11];Arrays.fill(frequencyA,0);
- int frequencyB[] = new int[11];Arrays.fill(frequencyB,0);
- int frequencyC[] = new int[11];Arrays.fill(frequencyC,0);
- int frequencyD[] = new int[11];Arrays.fill(frequencyD,0);
- int frequencyE[] = new int[11];Arrays.fill(frequencyE,0);
- int iteration = 100000;
- for(int i = 0;i<100000;i++)
- {
- int a = betterApproach();frequencyA[a]++;
- int b = centrallyBiased();frequencyB[b]++;
- int c = roundOffError1();frequencyC[c]++;
- int d = roundOffError2();frequencyD[d]++;
- int e = roundOffError3();frequencyE[e]++;
- }
- for(int i=0;i<11;i++) System.out.printf("%10d",i);
- System.out.println("");
- for(int i=0;i<11;i++) System.out.printf("%9.2f%%",(frequencyA[i]*100.0)/iteration);
- System.out.println("");
- for(int i=0;i<11;i++) System.out.printf("%9.2f%%",((double)frequencyB[i]*100.0)/iteration);
- System.out.println("");
- for(int i=0;i<11;i++) System.out.printf("%9.2f%%",((double)frequencyC[i]*100.0)/iteration);
- System.out.println("");
- for(int i=0;i<11;i++) System.out.printf("%9.2f%%",((double)frequencyD[i]*100.0)/iteration);
- System.out.println("");
- for(int i=0;i<11;i++) System.out.printf("%9.2f%%",((double)frequencyE[i]*100.0)/iteration);
- }
- static class Reader {
- public BufferedReader reader;
- public StringTokenizer tokenizer;
- public Reader() {
- }
- public Reader InitConsoleReading()
- {
- reader = new BufferedReader(new InputStreamReader(System.in), 32768);
- tokenizer = null;
- return this;
- }
- public Reader InitFileReading(String filePath) throws FileNotFoundException {
- reader = new BufferedReader(new FileReader(filePath), 32768);
- tokenizer = null;
- return this;
- }
- public String next() {
- while (tokenizer == null || !tokenizer.hasMoreTokens()) {
- try {
- tokenizer = new StringTokenizer(reader.readLine());
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- return tokenizer.nextToken();
- }
- public int nextInt() {
- return Integer.parseInt(next());
- }
- public long nextLong() {
- return Long.parseLong(next());
- }
- }
- static class Writer {
- private BufferedWriter log;
- public Writer InitConsoleWriting()
- {
- log = new BufferedWriter(new OutputStreamWriter(System.out));
- return this;
- }
- public Writer InitFileWriting(String filePath) throws IOException {
- log = new BufferedWriter(new FileWriter(filePath));
- return this;
- }
- public Writer WriteInt(int inp) throws IOException {
- this.log.write(String.valueOf(inp));
- return this;
- }
- public Writer WriteChar(char inp) throws IOException {
- this.log.write(inp);
- return this;
- }
- public Writer WriteNewLine() throws IOException {
- return this.WriteChar('\n');
- }
- public Writer WriteStr(String str) throws IOException {
- this.log.write(str);
- return this;
- }
- public Writer WriteIntArray(int inp[], char delimChar) throws IOException {
- int len = inp.length;
- if(len>0)
- {
- log.write(String.valueOf(inp[0]));
- for(int i=1;i<len;i++)
- {
- log.write(String.format("%c%s",delimChar,String.valueOf(inp[i])));
- }
- }
- return this;
- }
- public Writer WriteCharArray(char inp[], char delimChar) throws IOException {
- int len = inp.length;
- if(len>0)
- {
- log.write(String.valueOf(inp[0]));
- for(int i=1;i<len;i++)
- {
- log.write(String.format("%c%s",delimChar,String.valueOf(inp[i])));
- }
- }
- return this;
- }
- //
- public Writer WriteStrArray(String inp[], char delimChar) throws IOException {
- int len = inp.length;
- if(len>0)
- {
- log.write(inp[0]);
- for(int i=1;i<len;i++)
- {
- log.write(String.format("%c%s",delimChar,inp[i]));
- }
- }
- return this;
- }
- public Writer WriteCase(String prefix,int caseNo,String suffix) throws IOException {
- return this.WriteStr(String.format("%s%d%s",prefix,caseNo,suffix));
- }
- public Writer Dispose() throws IOException {
- this.log.flush();
- this.log.close();
- return this;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment