cern.jet.random.engine.MersenneTwister Java Examples

The following examples show how to use cern.jet.random.engine.MersenneTwister. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: IndependentPermutations.java    From CostFed with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Create n  permutations
 *
 */
private  void init() {



    if (a==null) {
    //    System.out.println("this.seed = "+this.seed);
        MersenneTwister random = new MersenneTwister((int) this.seed);
  a = new long[n];
  b = new long[n];
  for (int j=0; j<n; j++) {
      a[j] = random.nextLong();
      b[j] = random.nextLong();
 //     System.out.println("a["+j+"]="+a[j]);
  //    System.out.println("b["+j+"]="+b[j]);
      while (a[j]<0)  a[j] = random.nextLong();
      while (b[j]<0)  b[j] = random.nextLong();
  }  
    }
}
 
Example #2
Source File: IndependentNormalDistributionSampler.java    From beast-mcmc with GNU Lesser General Public License v2.1 6 votes vote down vote up
public IndependentNormalDistributionSampler(Variable variable, NormalDistributionModel model, double weight, boolean updateAllIndependently) {
	
	this.variable = variable;
	this.model = model;
	this.updateAllIndependently = updateAllIndependently;
	setWeight(weight);
	
	if (TRY_COLT) {
           randomEngine = new MersenneTwister(MathUtils.nextInt());
           //create standard normal distribution, internal states will be bypassed anyway
           coltNormal = new Normal(0.0, 1.0, randomEngine);
       } else {
       	//no random draw with specified mean and stdev implemented in the normal distribution in BEAST (as far as I know)
       	throw new RuntimeException("Normal distribution in BEAST still needs a random sampler.");
       }
	
}
 
Example #3
Source File: GibbsIndependentNormalDistributionOperator.java    From beast-mcmc with GNU Lesser General Public License v2.1 6 votes vote down vote up
public GibbsIndependentNormalDistributionOperator(Variable variable, NormalDistributionModel model, double weight, boolean updateAllIndependently) {
	
	this.variable = variable;
	this.model = model;
	this.updateAllIndependently = updateAllIndependently;
	setWeight(weight);
	
	if (TRY_COLT) {
           randomEngine = new MersenneTwister(MathUtils.nextInt());
           //create standard normal distribution, internal states will be bypassed anyway
           //takes mean and standard deviation
           coltNormal = new Normal(0.0, 1.0, randomEngine);
       } else {
       	//no random draw with specified mean and stdev implemented in the normal distribution in BEAST (as far as I know)
       	throw new RuntimeException("Normal distribution in BEAST still needs a random sampler.");
       }
	
}
 
Example #4
Source File: GibbsIndependentJointNormalGammaOperator.java    From beast-mcmc with GNU Lesser General Public License v2.1 6 votes vote down vote up
public GibbsIndependentJointNormalGammaOperator(Variable mean, Variable precision, NormalDistributionModel model, GammaDistribution gamma, double weight, boolean updateAllIndependently) {
	
	this.mean = mean;
       this.precision = precision;
	this.model = model;
       this.gamma = gamma;
	this.updateAllIndependently = updateAllIndependently;
	setWeight(weight);
	
	if (TRY_COLT) {
           randomEngine = new MersenneTwister(MathUtils.nextInt());
           //create standard normal distribution, internal states will be bypassed anyway
           //takes mean and standard deviation
           coltNormal = new Normal(0.0, 1.0, randomEngine);
           //coltGamma = new Gamma(gamma.getShape(), 1.0/gamma.getScale(), randomEngine);
       } else {
       	//no random draw with specified mean and stdev implemented in the normal distribution in BEAST (as far as I know)
       	throw new RuntimeException("Normal distribution in BEAST still needs a random sampler.");
       }
	
}
 
Example #5
Source File: IntBloomFilter.java    From database with GNU General Public License v2.0 6 votes vote down vote up
/** Creates a new Bloom filter with given number of hash functions and expected number of elements.
 * 
 * @param n the expected number of elements.
 * @param d the number of hash functions; if the filter add not more than <code>n</code> elements,
 * false positives will happen with probability 2<sup>-<var>d</var></sup>.
 */
public IntBloomFilter( final int n, final int d ) {
	this.d = d;
	bits = LongArrayBitVector.getInstance().length( (long)Math.ceil( ( n * d / NATURAL_LOG_OF_2 ) ) );
	m = bits.length() * Long.SIZE;

	if ( DEBUG ) System.err.println( "Number of bits: " + m );
	
	// The purpose of Random().nextInt() is to generate a different seed at each invocation.
	final MersenneTwister mersenneTwister = new MersenneTwister( new Random().nextInt() );
	a = new int[ d ];
	b = new int[ d ];
	for( int i = 0; i < d; i++ ) {
		a[ i ] = mersenneTwister.nextInt();
		b[ i ] = mersenneTwister.nextInt();
	}
}
 
Example #6
Source File: BloomFilter.java    From database with GNU General Public License v2.0 6 votes vote down vote up
/** Creates a new Bloom filter with given number of hash functions and expected number of elements.
 * 
 * @param n the expected number of elements.
 * @param d the number of hash functions; under obvious uniformity and indipendence assumptions,
 * if the filter has not more than <code>n</code> elements,
 * false positives will happen with probability 2<sup>-<var>d</var></sup>.
 */
public BloomFilter( final int n, final int d ) {
	this.d = d;
	final long wantedNumberOfBits = (long)Math.ceil( n * ( d / NATURAL_LOG_OF_2 ) );
	if ( wantedNumberOfBits > MAX_BITS ) throw new IllegalArgumentException( "The wanted number of bits (" + wantedNumberOfBits + ") is larger than " + MAX_BITS );
	bits = new long[ (int)( ( wantedNumberOfBits + Long.SIZE - 1 ) / Long.SIZE ) ];
	m = bits.length * (long)Long.SIZE;

	if ( DEBUG ) System.err.println( "Number of bits: " + m );
	
	// The purpose of Random().nextInt() is to generate a different seed at each invocation.
	final MersenneTwister mersenneTwister = new MersenneTwister( new Random().nextInt() );
	weight = new int[ d ][];
	init = new int[ d ];
	for( int i = 0; i < d; i++ ) {
		weight[ i ] = new int[ NUMBER_OF_WEIGHTS ];
		init[ i ] = mersenneTwister.nextInt();
		for( int j = 0; j < NUMBER_OF_WEIGHTS; j++ )
			 weight[ i ][ j ] = mersenneTwister.nextInt();
	}
}
 
Example #7
Source File: BatchedThompsonSamplingTest.java    From thompson-sampling with Apache License 2.0 6 votes vote down vote up
@Test
@Ignore
public void testCorrectArmChosen() {
  int correct = 0;
  for (int i = 0; i< 10000; i++) {
    RandomEngine engine = new MersenneTwister(i);
    BatchedThompsonSampling batchedBandit = new BatchedThompsonSampling();
    batchedBandit.setRandomEngine(engine);
    BatchedBanditTester tester = new BatchedBanditTester(batchedBandit, engine);
    if (i % 100 == 0) {
      System.out.println("Batches complete " + i);
    }
    correct += tester.getWinningArm();
  }
  System.out.println(correct);
  assertTrue(correct > 9500);
}
 
Example #8
Source File: BatchedABTestTest.java    From thompson-sampling with Apache License 2.0 6 votes vote down vote up
@Test
public void testCorrectArmChosen() {
  int correct = 0;
  for (int i = 0; i<= 10000; i++) {
    RandomEngine engine = new MersenneTwister(i);
    BanditPerformance performance = new BanditPerformance(2);
    BatchedABTest batchedBandit = new BatchedABTest();
    batchedBandit.setRandomEngine(engine);
    BatchedBanditTester tester = new BatchedBanditTester(batchedBandit, engine);
    if (i % 100 == 0) {
      System.out.println("Batches complete " + i);
    }
    correct += tester.getWinningArm();
  }
  assertTrue(correct > 9500);
}
 
Example #9
Source File: PoissonDistr.java    From EdgeCloudSim with GNU General Public License v3.0 5 votes vote down vote up
/**
   * Creates a new exponential number generator.
   * 
   * @param mean the mean for the distribution.
   */
  public PoissonDistr(double mean) {
engine = new MersenneTwister(new Date());
poisson = new Poisson(mean, engine);

//always sleep for some milliseconds in order not to have same seed for iterative PoissonDistr contruction
try {
	TimeUnit.MILLISECONDS.sleep(10);
} catch (InterruptedException e) {
   	SimLogger.printLine("impossible is occured! Poisson random number cannot be created!");
	e.printStackTrace();
   	System.exit(0);
}
  }
 
Example #10
Source File: GammaDistribution.java    From beast-mcmc with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Constructor
 */
public GammaDistribution(double shape, double scale) {
    this.shape = shape;
    this.scale = scale;
    this.samples = 0;

    if (TRY_COLT) {
        randomEngine = new MersenneTwister(MathUtils.nextInt());
        System.out.println("Colt Gamma(" + shape + "," + scale + ")");
        coltGamma = new Gamma(shape, 1.0/scale, randomEngine);
    }
}