-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandom_Int_Mean.java
More file actions
42 lines (36 loc) · 1.18 KB
/
Random_Int_Mean.java
File metadata and controls
42 lines (36 loc) · 1.18 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
import java.util.Random;
public final class Random_Int_Mean
{
private static int initialized = 0;
private static Random r = new Random();
public static void init_random_int(int seed)
{
/*
* Initializes the random number generator. If seed is negative then the
* system clock is used to initialize the generator. A count is also kept
* of the number of times this routine has been called.
*/
if (seed < 0) seed = (int) (System.currentTimeMillis() / 1000);
r.setSeed(seed);
initialized++;
}
public static int random_int(int mean)
{
/*
* Computes a random integer from an exponetial distribution with a
* specified mean. If this routine is called and the generator has not
* yet been initialized, it initializes it using the system clock.
*/
if (initialized == 0) init_random_int(-1);
/*
* Find number from exponentially distribution with specified mean
* and round to an integer.
*/
int value = (int) (0.5 - mean * Math.log(r.nextDouble()));
if (value == 0)
value = 1;
else
if (value > 5 * mean) value = 5 * mean;
return value;
}
}