Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.DataOutputStream;
- import java.io.FileOutputStream;
- import java.io.*;
- import java.util.*;
- import java.lang.*;
- public class rng {
- public static void main(String[] args) throws Exception {
- int size = 1000000; //number of trials
- int zeroNoFloorCounter=0; //counting how often a 0 occurs WITHOUT flooring the Math.random()
- int zeroWithFloorCounter=0; //counting how often a 0 occurs WITH flooring the Math.random()
- File file1 = new File("./rng_no_floor.txt");
- File file2 = new File("./rng_with_floor.txt");
- if(file1.exists())
- file1.delete();
- if(file2.exists())
- file2.delete();
- for(int i=0; i<size; i++){
- BufferedWriter out1 = new BufferedWriter(new FileWriter("./rng_no_floor.txt",true));
- BufferedWriter out2 = new BufferedWriter(new FileWriter("./rng_with_floor.txt",true));
- // get two random double numbers
- double x = Math.random()*5000;
- double y = (int) (Math.random()*5000);
- if(x==0)
- zeroNoFloorCounter++;
- if(y==0)
- zeroWithFloorCounter++;
- String xval = String.valueOf(x);
- String yval = String.valueOf(y);
- try {
- out1.write(xval);
- out1.newLine();
- out1.close();
- out2.write(yval);
- out2.newLine();
- out2.close();
- }
- catch (IOException e)
- {
- System.out.println("Exception ");
- }
- }
- System.out.println(zeroNoFloorCounter);
- System.out.println(zeroWithFloorCounter);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement