-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandom.java
More file actions
140 lines (124 loc) · 3.77 KB
/
Random.java
File metadata and controls
140 lines (124 loc) · 3.77 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Random class
//
// CONSTRUCTION: with (a) no initializer or (b) an integer
// that specifies the initial state of the generator
//
// ******************PUBLIC OPERATIONS*********************
// Return a random number according to some distribution:
// int randomInt( ) --> Uniform, 1 to 2^31-1
// int random0_1( ) --> Uniform, 0 to 1
// int randomInt( int low, int high ) --> Uniform low..high
// long randomLong( long low, long high ) --> Uniform low..high
// void permute( Object [ ] a ) --> Randomly permutate
/**
* Random number class, using a 31-bit
* linear congruential generator.
* Note that java.util contains a class Random,
* so watch out for name conflicts.
* @author Mark Allen Weiss
*/
public class Random
{
private static final int A = 48271;
private static final int M = 2147483647;
private static final int Q = M / A;
private static final int R = M % A;
/**
* Construct this Random object with
* initial state obtained from system clock.
*/
public Random( )
{
this( (int) ( System.currentTimeMillis( ) % Integer.MAX_VALUE ) );
}
/**
* Construct this Random object with
* specified initial state.
* @param initialValue the initial state.
*/
public Random( int initialValue )
{
if( initialValue < 0 )
initialValue += M;
state = initialValue;
if( state == 0 )
state = 1;
}
/**
* Return a pseudorandom int, and change the
* internal state.
* @return the pseudorandom int.
*/
public int randomInt( )
{
int tmpState = A * ( state % Q ) - R * ( state / Q );
if( tmpState >= 0 )
state = tmpState;
else
state = tmpState + M;
return state;
}
/**
* Return a pseudorandom int, and change the
* internal state. DOES NOT WORK.
* @return the pseudorandom int.
*/
public int randomIntWRONG( )
{
return state = ( A * state ) % M;
}
/**
* Return a pseudorandom double in the open range 0..1
* and change the internal state.
* @return the pseudorandom double.
*/
public double random0_1( )
{
return (double) randomInt( ) / M;
}
/**
* Return an int in the closed range [low,high], and
* change the internal state.
* @param low the minimum value returned.
* @param high the maximum value returned.
* @return the pseudorandom int.
*/
public int randomInt( int low, int high )
{
double partitionSize = (double) M / ( high - low + 1 );
return (int) ( randomInt( ) / partitionSize ) + low;
}
/**
* Return an long in the closed range [low,high], and
* change the internal state.
* @param low the minimum value returned.
* @param high the maximum value returned.
* @return the pseudorandom long.
*/
public long randomLong( long low, long high )
{
long longVal = ( (long) randomInt( ) << 31 ) + randomInt( );
long longM = ( (long) M << 31 ) + M;
double partitionSize = (double) longM / ( high - low + 1 );
return (long) ( longVal / partitionSize ) + low;
}
/**
* Randomly rearrange an array.
* The random numbers used depend on the time and day.
* @param a the array.
*/
public static void permute( Object [ ] a )
{
Random r = new Random( );
for( int j = 1; j < a.length; j++ )
Sort.swapReferences( a, j, r.randomInt( 0, j ) );
}
private int state;
// Test program
public static void main( String [ ] args )
{
Random r = new Random( 1 );
for( int i = 0; i < 20; i++ )
System.out.println( r.randomInt( ) );
}
}