-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandom_Gen.java
55 lines (50 loc) · 1.64 KB
/
Random_Gen.java
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.util.Random;
import java.io.FileWriter;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.ArrayList;
class Random_Gen extends Save_Location
{
public static void main(int range){
int per_set=range;
int result[] = new int[per_set];
generate(range, per_set, result);
write(per_set, result);
}
private static void generate(int range, int per_set, int[]result){
ArrayList<Integer> list = new ArrayList<Integer>(range);
for(int i = 1; i <= range; i++) {
list.add(i);
}
int x = 0;
Random rand = new Random();
while(list.size() > 0) {
int index = rand.nextInt(list.size());
result[x++]=list.remove(index);
}
}
private static void write(int per_set, int[]result){
//----------------------file creation and writing data---------------------------
try {
File ftx = new File(file_source);
if (ftx.createNewFile()) {
System.out.println("File created: " + ftx.getName());
} else {
System.out.println("File already exists. It will be overridden.");
}
} catch (IOException e) {
System.out.println("ERROR");
e.printStackTrace();
}
try{
FileWriter writer = new FileWriter(file_source);
for (int i = 0; i < per_set; i++)
writer.write(result[i]+"\n");
writer.close();
}catch(Exception e)
{
System.out.println("An error occured while writing");
}
}
}