-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuantumRand.java
75 lines (59 loc) · 1.51 KB
/
QuantumRand.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
* Title: Quantum Random
* Author: Aleksandar Damnjanovic
* Date: 21.11.2021
*/
import java.io.IOException;
import java.math.BigInteger;
import java.net.URL;
import java.util.Scanner;
import org.json.JSONArray;
import org.json.JSONObject;
public class QuantumRand {
private long value=-1;
private String address="https://qrng.anu.edu.au/API/jsonI.php?length=1&type=hex16&size=8";
public QuantumRand() {
next();
}
public long getLong() {
return value;
}
public long next() {
try {
URL url= new URL(address);
Scanner scan= new Scanner(url.openStream());
String content = scan.next();
JSONObject obj= new JSONObject(content);
JSONArray array= (JSONArray) obj.get("data");
String num= array.get(0).toString();
BigInteger bi = new BigInteger(num, 16);
long val= bi.longValue();
value= Math.abs(val);
return value;
} catch (IOException e) {
e.printStackTrace();
}
return -1;
}
public long getLong(long min, long max) {
if(min<0 || max>Long.MAX_VALUE) {
System.out.println("min must be bigger than 0\nmax must be less that "+Long.MAX_VALUE);
return -1;
}
long ret=0;
if(max!= Long.MAX_VALUE)
if(this.value> max)
ret= this.value %max;
if(ret<min && ret!=0)
ret= min + (this.value % (max-min));
return ret;
}
public double getFloat() {
double ret=0;
if(this.value<Long.MAX_VALUE)
ret= (double)(this.value) / (double)(Long.MAX_VALUE);
else
ret=(double)(Long.MAX_VALUE) / (double)(this.value);
return ret;
}
}