Author:
moogie (posted
2012-06-15 18:03:19 , viewed 348 times)
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
public class RandomIteratorTest {
interface CreateRandomIteratorInstance
{
public RandomIterator createRandomIteratorInstance (long seed );
}
public static void main (String [] args ) {
final int listSize = 1 << 20 ;
final int iterations = 100 ;
final List <Integer > list = new ArrayList <Integer >(listSize );
for (int i = 0 ; i < listSize ; i ++) {
list .add (i );
}
collisionCheck (list ,iterations );
speedTest (list .size (),iterations );
}
static private void collisionCheck (final List <Integer > list ,final int iterations )
{
HashSet <Integer > seenSet = new HashSet <Integer >(list .size ());
runCollisionTest (iterations ,seenSet ,list , new CreateRandomIteratorInstance () {
@Override
public RandomIterator createRandomIteratorInstance (long seed ) {
return new SemiRandomIteratorPrimeStep (list , new Random (seed ),seed );
}
});
runCollisionTest (iterations ,seenSet ,list , new CreateRandomIteratorInstance () {
@Override
public RandomIterator createRandomIteratorInstance (long seed ) {
return new SemiRandomIteratorBucketRiven (list , 333 ,new Random (seed ),seed );
}
});
}
static private void speedTest (final int listSize ,final int iterations )
{
runIndexOnlyTest (iterations ,listSize , new CreateRandomIteratorInstance () {
@Override
public RandomIterator createRandomIteratorInstance (long seed ) {
return new SemiRandomIteratorPrimeStep (listSize , new Random (seed ),seed );
}
});
runIndexOnlyTest (iterations ,listSize , new CreateRandomIteratorInstance () {
@Override
public RandomIterator createRandomIteratorInstance (long seed ) {
return new SemiRandomIteratorBucketRiven (listSize , 333 ,new Random (seed ),seed );
}
});
}
static private void runCollisionTest (final int iterations ,final HashSet <Integer > seenSet , final List <Integer > list , CreateRandomIteratorInstance createRandomIteratorInstance )
{
RandomIterator randomIterator = null ;
for (int j = 0 ; j < iterations ; j ++) {
seenSet .clear ();
randomIterator = createRandomIteratorInstance .createRandomIteratorInstance (j );
for (int i = 0 ; i < list .size (); i ++) {
Integer obj = randomIterator .getNext ();
if (seenSet .contains (obj )) {
System .out .println (randomIterator .getName ()+" collision. " +randomIterator .getStatus ());
System .exit (1 );
}
seenSet .add (obj );
}
}
System .out .println (randomIterator .getName ()+" no collisions." );
}
static private void runIndexOnlyTest (final int iterations , final int listSize , CreateRandomIteratorInstance createRandomIteratorInstance )
{
RandomIterator randomIterator = null ;
long startTime = System .currentTimeMillis ();
for (int j = 0 ; j < iterations ; j ++) {
randomIterator = createRandomIteratorInstance .createRandomIteratorInstance (j );
for (int i = 0 ; i < listSize ; i ++) {
int index = randomIterator .getNextIndex ();
}
}
System .out .println (randomIterator .getName ()+" time taken = " +(System .currentTimeMillis ()-startTime ));
}
}
class SemiRandomIteratorBucketRiven implements RandomIterator {
final Integer [] bucket ;
int currentBucket = 0 ;
final int bucketSize ;
final List <Integer > remainder = new ArrayList <Integer >();
final int bucketCount ;
final long seed ;
final List <Integer > list ;
int subBucketIndex = 0 ;
final Random random ;
SemiRandomIteratorBucketRiven (List <Integer > list , int bucketCount ,
Random random , long seed ) {
this .bucketCount = bucketCount ;
this .list = list ;
this .seed = seed ;
this .random = random ;
bucketSize = list .size () / bucketCount ;
bucket = (Integer []) new Integer [bucketSize ];
}
SemiRandomIteratorBucketRiven (int collectionSize , int bucketCount ,
Random random , long seed ) {
list = new ArrayList <Integer >(collectionSize );
for (int i = 0 ; i < collectionSize ; i ++) {
list .add (i );
}
this .bucketCount = bucketCount ;
this .seed = seed ;
this .random = random ;
bucketSize = list .size () / bucketCount ;
bucket = (Integer []) new Integer [bucketSize ];
}
@Override
public Integer getNext () {
if (currentBucket == bucketCount ) {
if (subBucketIndex == 0 ) {
if (list .size () % bucketCount != 0 ) {
if (bucket .length == 0 ) {
remainder .addAll (list );
} else {
for (int k = 0 ; k < bucketCount ; k ++) {
for (int i = k + bucket .length * bucketCount ; i < list
.size (); i += bucketCount ) {
remainder .add (list .get (i ));
}
}
}
Collections .shuffle (remainder );
}
}
return remainder .get (subBucketIndex ++);
} else {
if (subBucketIndex == bucketSize ) {
subBucketIndex = 0 ;
currentBucket ++;
return getNext ();
} else if (subBucketIndex == 0 ) {
for (int i = 0 , p = currentBucket ; i < bucket .length ; i ++, p += bucketCount ) {
bucket [i ] = list .get (p );
}
;
}
int p = subBucketIndex
+ random .nextInt (bucketSize - subBucketIndex );
Integer returnInteger = bucket [p ];
bucket [p ] = bucket [subBucketIndex ];
subBucketIndex ++;
return returnInteger ;
}
}
@Override
public String getStatus () {
return "subBucketIndex: " + subBucketIndex + " currentBucket: "
+ currentBucket + " seed: " + seed ;
}
@Override
public int getNextIndex () {
return getNext ();
}
public String getName ()
{
return "Bucket Riven" ;
}
}
class SemiRandomIteratorPrimeStep extends IntervalRandomIterator {
public SemiRandomIteratorPrimeStep (List <Integer > collection , Random random ,long seed ) {
super (collection ,createInterval (collection .size (),random ),random ,seed );
}
public SemiRandomIteratorPrimeStep (int collectionSize , Random random ,long seed ) {
super (collectionSize ,createInterval (collectionSize ,random ),random ,seed );
}
private static int createInterval (int collectionSize , Random random )
{
final int bitLength = 24 ;
return BigInteger .probablePrime (bitLength , random ).intValue ();
}
public String getName ()
{
return "Prime Step" ;
}
}
public interface RandomIterator {
public Integer getNext ();
public int getNextIndex ();
public String getStatus ();
public String getName ();
}
abstract class IntervalRandomIterator implements RandomIterator {
public int getStartIndex () {
return startIndex ;
}
public int getInterval () {
return interval ;
}
public int getCurrIndex () {
return currIndex ;
}
private final int startIndex ;
private final int interval ;
private final List <Integer > collection ;
private final int collectionSize ;
private int currIndex ;
private final long seed ;
public IntervalRandomIterator (List <Integer > collection , int interval ,Random random ,long seed ) {
this .collection = collection ;
collectionSize = collection .size ();
this .interval =interval ;
startIndex = random .nextInt (collectionSize );
currIndex = startIndex ;
this .seed =seed ;
}
public IntervalRandomIterator (int collectionSize , int interval ,Random random ,long seed ) {
this .collection = null ;
this .collectionSize =collectionSize ;
this .interval =interval ;
startIndex = random .nextInt (collectionSize );
currIndex = startIndex ;
this .seed =seed ;
}
public Integer getNext () {
Integer o = collection .get (currIndex );
currIndex = (interval - (collectionSize - currIndex )) % collectionSize ;
return o ;
}
public int getNextIndex () {
int returnIndex = currIndex ;
currIndex = (interval - (collectionSize - currIndex )) % collectionSize ;
return returnIndex ;
}
public String getStatus ()
{
return "Index: "
+ getCurrIndex () + " interval: "
+ getInterval ()
+ " startIndex: "
+ getStartIndex () + " seed: "
+ seed ;
}
}
Special syntax:
To highlight a line (yellow background), prefix it with '@@'
To indicate that a line should be removed (red background), prefix it with '-'
To indicate that a line should be added (green background), prefix it with '+'
To post multiple snippets, seperate them by '~~~~'
ivj94
(583 views)
2018-03-24 14:47:39
ivj94
(48 views)
2018-03-24 14:46:31
ivj94
(374 views)
2018-03-24 14:43:53
Solater
(62 views)
2018-03-17 05:04:08
nelsongames
(109 views)
2018-03-05 17:56:34
Gornova
(151 views)
2018-03-02 22:15:33
buddyBro
(693 views)
2018-02-28 16:59:18
buddyBro
(92 views)
2018-02-28 16:45:17
xxMrPHDxx
(493 views)
2017-12-31 17:17:51
xxMrPHDxx
(733 views)
2017-12-31 17:15:51
java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org