Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (568)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: moogie (posted 2012-06-15 18:03:19, viewed 124 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);
   }
   /**
    * @param args
    */
   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());
      
      // primeStep
      
      runCollisionTest(iterations,seenSet,list, new CreateRandomIteratorInstance() {
         
         @Override
         public RandomIterator createRandomIteratorInstance(long seed) {
            return new SemiRandomIteratorPrimeStep(list, new Random(seed),seed);
         }
      });
      
      // BucketRiven
      
      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)
   {
      
      // primeStep
      
      runIndexOnlyTest(iterations,listSize, new CreateRandomIteratorInstance() {
         
         @Override
         public RandomIterator createRandomIteratorInstance(long seed) {
            return new SemiRandomIteratorPrimeStep(listSize, new Random(seed),seed);
         }
      });
      
      // BucketRiven
      
      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;
   }
}





Dump your java code here :



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 '~~~~'
  EOF
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (20 views)
2014-09-24 16:12:06

Grunnt (45 views)
2014-09-23 14:38:19

radar3301 (28 views)
2014-09-21 23:33:17

BurntPizza (64 views)
2014-09-21 02:42:18

BurntPizza (34 views)
2014-09-21 01:30:30

moogie (42 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!