-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWriteOnFile2.java
More file actions
27 lines (25 loc) · 1.07 KB
/
WriteOnFile2.java
File metadata and controls
27 lines (25 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*
This program writes random numbers on a file
*/
import java.io.*;
import java.util.*;
public class WriteOnFile2{
public static void main(String[] args)
throws FileNotFoundException{
PrintStream output = new PrintStream(new File ("RandomNumbers.txt"));
PrintStream output2 = new PrintStream(new File ("RandomNumbers2.txt"));
PrintStream output3 = new PrintStream(new File ("RandomNumbers3.txt"));
//PrintStream is a Java class. Here we have created a new object of type PrintStream
//tied to an output file
for (int i=0;i<100;i++){
double x = Math.random(); //this uses the random method from Math class
output.println(x);
//alternatively you can use the Random object
Random r = new Random();//creates a new Random object
x = r.nextDouble();//calls method nextDouble to generate a random number of double precision between 0 (inclusive) and 1 (exclusive)
output2.println(x);
int a = r.nextInt(100)+1;//calls method nextInt to generate an integer random number between 0(inclusive) and 100(exclusive)
output3.println(a);
}
}
}