Author: moogie (posted 2012-06-16 07:28:04, viewed 102 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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
| 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);
}
});
runCollisionTest(iterations,seenSet,list, new CreateRandomIteratorInstance() {
@Override
public RandomIterator createRandomIteratorInstance(long seed) {
return new SemiRandomIteratorPOTRoquen(list, 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);
}
});
runIndexOnlyTest(iterations,listSize, new CreateRandomIteratorInstance() {
@Override
public RandomIterator createRandomIteratorInstance(long seed) {
return new SemiRandomIteratorPOTRoquen(listSize, 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;
}
}
public class SemiRandomIteratorDirectionalUprightPath implements RandomIterator {
private int startIndex;
private int rangeStart;
private int rangeEnd;
private int currIndex;
private int currLoopIndex;
private int step;
private boolean up;
private boolean move;
private final List<Integer> collection;
private final Random random;
private final long seed;
private final int collectionSize;
public SemiRandomIteratorDirectionalUprightPath(List<Integer> collection,
Random random, long seed) {
this.collection = collection;
this.random = random;
this.seed = seed;
this.collectionSize = collection.size();
this.rangeStart = random.nextInt(collectionSize);
int range;
if (collectionSize%2==1)
{
range=collectionSize+1;
}
else
{
range=collectionSize;
}
this.rangeEnd = this.rangeStart + range;
this.startIndex = this.rangeStart + random.nextInt(range);
this.currIndex = startIndex;
this.currLoopIndex = this.currIndex;
this.step = range/2;
this.up = random.nextBoolean();
this.move = random.nextBoolean();
}
public SemiRandomIteratorDirectionalUprightPath(int collectionSize,
Random random, long seed) {
this.collection = null;
this.random = random;
this.seed = seed;
this.collectionSize = collection.size();
this.rangeStart = random.nextInt(collectionSize);
int range;
if (collectionSize%2==1)
{
range=collectionSize+1;
}
else
{
range=collectionSize;
}
this.rangeEnd = this.rangeStart + range;
this.startIndex = this.rangeStart + random.nextInt(range);
this.currIndex = startIndex;
this.currLoopIndex = this.currIndex;
this.step = range/2;
this.up = random.nextBoolean();
this.move = random.nextBoolean();
}
public Integer getNext() {
if (currIndex >= collectionSize) {
calculateNextIndex();
return getNext();
}
Integer o = collection.get(currIndex);
calculateNextIndex();
return o;
}
private void calculateNextIndex() {
if (up) {
currIndex += step;
if (currIndex >= rangeEnd) {
currIndex = rangeStart + (currIndex - rangeEnd);
}
} else {
currIndex -= step;
if (currIndex < rangeStart) {
currIndex = rangeEnd - (rangeStart - currIndex);
}
}
if (currLoopIndex == currIndex) {
currIndex += move ? 1 : -1;
if (currIndex >= rangeEnd) {
currIndex = rangeStart + (currIndex - rangeEnd);
} else if (currIndex < rangeStart) {
currIndex = rangeEnd - (rangeStart - currIndex);
}
currLoopIndex = currIndex;
}
}
@Override
public int getNextIndex() {
if (currIndex >= collectionSize) {
calculateNextIndex();
return getNextIndex();
}
int returnIndex = currIndex;
calculateNextIndex();
return returnIndex;
}
@Override
public String getStatus() {
return "no status";
}
@Override
public String getName() {
return "Directional by UprightPath";
}
}
public class SemiRandomIteratorPOTRoquen implements RandomIterator {
private final List<Integer> collection;
private final Random random;
private final long seed;
private final int collectionSize;
private final int length;
private int m;
private int a;
private int mask;
private int v;
public SemiRandomIteratorPOTRoquen(List<Integer> collection,Random random, long seed)
{
this.random=random;
this.collection=collection;
this.collectionSize=collection.size();
this.seed=seed;
length = 1<< ((int) Math.ceil(Math.log(collectionSize)/Math.log(2)));
setLength(length);
}
public SemiRandomIteratorPOTRoquen(int collectionSize,Random random, long seed)
{
this.random=random;
this.collection=null;
this.collectionSize=collectionSize;
this.seed=seed;
length = 1<< ((int) Math.ceil(Math.log(collectionSize)/Math.log(2)));
setLength(length);
}
public void setLength(int length)
{
mask = length-1;
reset();
}
public void reset()
{
m = (random.nextInt()<<3)|5; a = (random.nextInt()<<1)|1; v = (random.nextInt()); }
public Integer getNext() {
Integer o = collection.get(getNextIndex());
return o;
}
@Override
public int getNextIndex() {
do
{
v = (m*v + a) & mask;
} while (v>=collectionSize);
return v;
}
@Override
public String getStatus() {
return "index: "+v+" length: "+length+" seed: "+seed;
}
@Override
public String getName() {
return "Power of Two Perturbed Sequence by Roquen";
}
} |
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 '~~~~'
|
Add your game by posting it in the WIP section,
or publish it in Showcase.
The first screenshot will be displayed as a thumbnail.
|
|