Mersenne : Random « Development Class « Java






Mersenne

  
/*
 * @(#)Mersenne.java  1.0 Apr 26, 2008
 *
 *  The MIT License
 *
 *  Copyright (c) 2008 Malachi de AElfweald <malachid@gmail.com>
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal
 *  in the Software without restriction, including without limitation the rights
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in
 *  all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *  THE SOFTWARE.
 */
//package org.eoti.math.prime;

import java.math.BigInteger;
import java.util.*;

public class Mersenne
{
  protected static BigInteger two = BigInteger.valueOf(2);
  protected static String[] primes = new String[]{
      "2","3","5","7","13","17","19","31","61","89","107","127","521","607",    // M1-M14
      "1279","2203","2281","3217","4253","4423","9689","9941","11213", "19937",  // M15-M23
      "21701","23209","44497","86243","110503","132049","216091","756839",      // M24-M32
      "859433","1257787","1398269","2976221","3021377","6972593","13466917",    // M33-M39
      // indexes of rest could be wrong
      "20996011","24036583","25964951","30402457","32582657","37156667",      // M40-M45???
      "42643801", "43112609"                                    // M46-M47???
  };

  protected BigInteger value;
  protected int index;
  public Mersenne(int index)
  {
    this.index = index;
    value = two.pow(Integer.parseInt(primes[index-1]));
    value = value.subtract(BigInteger.ONE);
  }

  // Per common Mersenne Prime tables, index is 1-based
  public int getIndex(){return index;}

  public Integer getN(){return Integer.parseInt(primes[index-1]);}
  public Integer getBitLength(){return getN();}

  public Integer getDigitLength(){return (""+getValue()).length();}

  public BigInteger getValue(){return value;}

  public static Iterable<Mersenne> iterator()
  {
    return new Iterable<Mersenne>()
    {
      public Iterator<Mersenne> iterator()
      {
        return new MersenneIter();
      }
    };
  }

  static class MersenneIter
    implements Iterator<Mersenne>
  {
    protected int currIndex = 0;

    MersenneIter(){}

    public boolean hasNext()
    {
      return currIndex <= primes.length;
    }

    public Mersenne next()
    {
      return new Mersenne(++currIndex);
    }

    public void remove()
    {
      throw new UnsupportedOperationException("Remove not implemented");
    }
  }
}

   
    
  








Related examples in the same category

1.Create random number
2.Create two random number generators with the same seed
3.Does Math.random() produce 0.0 and 1.0
4.Random numbers between 1 and 100
5.Random number between 0 AND 10
6.Random integers that range from from 0 to n
7.Random.nextInt(n) returns a distributed int value between 0 (inclusive) and n (exclusive).
8.Round Java float and double numbers using Math.round
9.Randomizer
10.nextDouble() and nextGaussian() in java.util.Random
11.Generating random numbers
12.Generate random ints by asking Random() for
13.Getting random numbers: nextGaussian
14.Generate a random array of numbers
15.Next double and next int
16.Random bytes
17.Random boolean
18.Random long type number
19.Random float type number
20.Random double type number
21.Math.random
22.A wrapper that supports all possible Random methods via the java.lang.Math#random() method and its system-wide {@link Random} object.
23.Operations for random Strings
24.Random Util with ReentrantLock
25.A Java implementation of the MT19937 (Mersenne Twister) pseudo random number generator algorithm
26.Randomizer
27.Random: Properly synchronized and can be used in a multithreaded environment
28.A predictable random number generator.
29.Random GUID
30.A random.org seeded random generator.
31.Generates a secure random word with the given length consisting of uppercase and lowercase letters and numbers.
32.A random choice maker
33.Memory-efficient map of keys to values with list-style random-access semantics.
34.Generates a random integer inside the lo and hi interval
35.Randomizer
36.RandomGUID generates truly random GUIDs
37.A random access text file allows a text file to be accessed randomly starting at any position.
38.A garbage-free randomised quicksort
39.A linear random method based on xor shifts
40.Mersenne Twister Random
41.A GaussianHat is a random number generator that will give you numbers based on a population mean and standard deviation.
42.Randomly permutes the elements of the specified array
43.generate string consists of random characters.
44.Atomic Pseudo Random
45.Atomic Simple Random
46.Mersenne Twister
47.Mersenne Twister Fast
48.Mersenne Twister algorithm
49.Emulates a dice