Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. import java.io.DataOutputStream;
  2. import java.io.FileOutputStream;
  3. import java.io.*;
  4. import java.util.*;
  5. import java.lang.*;
  6.  
  7. public class rng {
  8. public static void main(String[] args) throws Exception {
  9.  
  10. int size = 1000000; //number of trials
  11. int zeroNoFloorCounter=0; //counting how often a 0 occurs WITHOUT flooring the Math.random()
  12. int zeroWithFloorCounter=0; //counting how often a 0 occurs WITH flooring the Math.random()
  13.  
  14. File file1 = new File("./rng_no_floor.txt");
  15. File file2 = new File("./rng_with_floor.txt");
  16.  
  17. if(file1.exists())
  18. file1.delete();
  19. if(file2.exists())
  20. file2.delete();
  21.  
  22. for(int i=0; i<size; i++){
  23.  
  24. BufferedWriter out1 = new BufferedWriter(new FileWriter("./rng_no_floor.txt",true));
  25. BufferedWriter out2 = new BufferedWriter(new FileWriter("./rng_with_floor.txt",true));
  26. // get two random double numbers
  27. double x = Math.random()*5000;
  28. double y = (int) (Math.random()*5000);
  29.  
  30. if(x==0)
  31. zeroNoFloorCounter++;
  32. if(y==0)
  33. zeroWithFloorCounter++;
  34.  
  35. String xval = String.valueOf(x);
  36. String yval = String.valueOf(y);
  37.  
  38. try {
  39. out1.write(xval);
  40. out1.newLine();
  41. out1.close();
  42.  
  43. out2.write(yval);
  44. out2.newLine();
  45. out2.close();
  46. }
  47. catch (IOException e)
  48. {
  49. System.out.println("Exception ");
  50. }
  51. }
  52. System.out.println(zeroNoFloorCounter);
  53. System.out.println(zeroWithFloorCounter);
  54.  
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement