forked from hacktoberfest17/programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandom.java
More file actions
59 lines (50 loc) · 1.14 KB
/
Random.java
File metadata and controls
59 lines (50 loc) · 1.14 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
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
/**
* Simple random number generator
* Code adapted from OOP class slides.
* @author Felipe Scrochio Custódio
*/
import java.util.Calendar;
public class Random {
private long p = 2147483648l;
private long m = 843314861;
private long a = 453816693;
private long xi = 1023;
/**
* Returns a random number in the interval (0,1[
* @return Random number
*/
public double getRand() {
xi = (a + m * xi) % p;
double d = xi;
return d / p;
}
/**
* Returns a random int in the interval (0,max[
* @param max - Limit
* @return Random number
*/
public int getIntRand(int max) {
double d = getRand() * max;
return (int) d;
}
/**
* Allows changing the seed for the random number generator.
* @param seed - the new value for the seed
*/
public void setSemente(int seed) {
xi = seed;
}
/**
* Constructor that allows setting the seed beforehand
* @param k - Initial value for the seed
*/
public Random(int k) {
xi = k;
}
/**
* Constructor that uses a random seed, generated by getting the current time in the machine (millisseconds)
*/
public Random() {
xi = Calendar.getInstance().getTimeInMillis();
}
}