本文整理汇总了Java中cern.jet.random.engine.RandomEngine类的典型用法代码示例。如果您正苦于以下问题:Java RandomEngine类的具体用法?Java RandomEngine怎么用?Java RandomEngine使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
RandomEngine类属于cern.jet.random.engine包,在下文中一共展示了RandomEngine类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import cern.jet.random.engine.RandomEngine; //导入依赖的package包/类
public static void main(String[] args) {
MeterRegistry registry = SampleConfig.myMonitoringSystem();
Counter counter = registry.counter("counter");
AtomicInteger n = new AtomicInteger(0);
registry.more().counter("fcounter", emptyList(), n);
RandomEngine r = new MersenneTwister64(0);
Normal dist = new Normal(0, 1, r);
Flux.interval(Duration.ofMillis(10))
.doOnEach(d -> {
if (dist.nextDouble() + 0.1 > 0) {
counter.increment();
n.incrementAndGet();
}
})
.blockLast();
}
示例2: testFlat
import cern.jet.random.engine.RandomEngine; //导入依赖的package包/类
@Test
//TODO if this interpolator cannot get the answer right then an exception should be thrown
public void testFlat() {
final RandomEngine random = new MersenneTwister64(MersenneTwister.DEFAULT_SEED);
final double x1 = 10 * random.nextDouble();
final double x2 = 10 * random.nextDouble();
final double x3 = 10 * random.nextDouble();
// Fails utterly for flat surface since the variogram function will be zero for all r
final InterpolatorND interpolator = new KrigingInterpolatorND(1.99);
final InterpolatorNDDataBundle dataBundle = interpolator.getDataBundle(FLAT_DATA);
assertEquals(INTERPOLATOR.interpolate(dataBundle, new double[] {x1, x2, x3}), 0, 0);
}
示例3: sample
import cern.jet.random.engine.RandomEngine; //导入依赖的package包/类
/**
* Uniformly samples (chooses) <tt>n</tt> random elements <i>with or without replacement</i> from the contained elements and adds them to the given buffer.
* If the buffer is connected to a bin, the effect is that the chosen elements are added to the bin connected to the buffer.
* Also see {@link #buffered(int) buffered}.
*
* @param n the number of elements to choose.
* @param withReplacement <tt>true</tt> samples with replacement, otherwise samples without replacement.
* @param randomGenerator a random number generator. Set this parameter to <tt>null</tt> to use a default random number generator seeded with the current time.
* @param buffer the buffer to which chosen elements will be added.
* @throws IllegalArgumentException if <tt>!withReplacement && n > size()</tt>.
* @see cern.jet.random.sampling
*/
public synchronized void sample(int n, boolean withReplacement, RandomEngine randomGenerator, cern.colt.buffer.DoubleBuffer buffer) {
if (randomGenerator==null) randomGenerator = cern.jet.random.Uniform.makeDefaultGenerator();
buffer.clear();
if (!withReplacement) { // without
if (n>size()) throw new IllegalArgumentException("n must be less than or equal to size()");
cern.jet.random.sampling.RandomSamplingAssistant sampler = new cern.jet.random.sampling.RandomSamplingAssistant(n,size(),randomGenerator);
for (int i=n; --i >= 0; ) {
if (sampler.sampleNextElement()) buffer.add(this.elements.getQuick(i));
}
}
else { // with
cern.jet.random.Uniform uniform = new cern.jet.random.Uniform(randomGenerator);
int s = size();
for (int i=n; --i >= 0; ) {
buffer.add(this.elements.getQuick(uniform.nextIntFromTo(0,s-1)));
}
buffer.flush();
}
}
示例4: EuclidianFactory
import cern.jet.random.engine.RandomEngine; //导入依赖的package包/类
public EuclidianFactory(int dimension, int n, double standardDeviation) {
this.dimension = dimension;
this.n = n;
RandomEngine engine = new DRand();
Normal normal = new Normal(0, standardDeviation, engine);
allElements = new ArrayList(n);
for (int i=0; i < n; i++) {
double x[] = new double[dimension];
for (int j=0; j < dimension; j++) {
x[j] = normal.nextDouble();
}
allElements.add(new Euclidean(x));
}
}
示例5: testOffer
import cern.jet.random.engine.RandomEngine; //导入依赖的package包/类
@Test
public void testOffer() throws QuantilesException {
System.out.println("offer");
double[] quantiles = new double[]{0.05, 0.25, 0.5, 0.75, 0.95};
Frugal2U instance = new Frugal2U(quantiles, 0);
ExactQuantilesAll<Integer> exact = new ExactQuantilesAll<Integer>();
RandomEngine r = new MersenneTwister64(0);
Normal dist = new Normal(100, 50, r);
int numSamples = 1000;
for(int i = 0; i < numSamples; ++i) {
int num = (int) Math.max(0, dist.nextDouble());
instance.offer(num);
exact.offer(num);
}
System.out.println("Q\tEst\tExact");
for (double q : quantiles) {
System.out.println(q + "\t" + instance.getQuantile(q) + "\t" + exact.getQuantile(q));
}
}
示例6: testGeometricDistribution
import cern.jet.random.engine.RandomEngine; //导入依赖的package包/类
@Test
public void testGeometricDistribution()
{
StreamSummary<Integer> vs = new StreamSummary<Integer>(10);
RandomEngine re = RandomEngine.makeDefault();
for (int i = 0; i < NUM_ITERATIONS; i++)
{
int z = Distributions.nextGeometric(0.25, re);
vs.add(z);
}
List<CountEntry<Integer>> top = vs.peek(5);
System.out.println("Geometric:");
for (CountEntry<Integer> e : top)
{
System.out.println(e);
}
CountEntry<Integer> tippyTop = top.get(0);
assertEquals(0, (int) tippyTop.getItem());
System.out.println(vs);
}
示例7: testGeometricDistribution
import cern.jet.random.engine.RandomEngine; //导入依赖的package包/类
@Test
public void testGeometricDistribution() {
ConcurrentStreamSummary<Integer> vs = new ConcurrentStreamSummary<Integer>(10);
RandomEngine re = RandomEngine.makeDefault();
for (int i = 0; i < NUM_ITERATIONS; i++) {
int z = Distributions.nextGeometric(0.25, re);
vs.add(z);
}
List<CountEntry<Integer>> top = vs.peek(5);
System.out.println("Geometric:");
for (CountEntry<Integer> e : top) {
System.out.println(e.getItem() + "\t" + e.getFrequency());
}
CountEntry<Integer> tippyTop = top.get(0);
assertEquals(0, (int) tippyTop.getItem());
System.out.println(vs);
}
示例8: testZipfianDistribution
import cern.jet.random.engine.RandomEngine; //导入依赖的package包/类
@Test
public void testZipfianDistribution()
{
RandomEngine re = RandomEngine.makeDefault();
for (int i = 0; i < NUM_ITERATIONS; i++)
{
int z = Distributions.nextZipfInt(1.2D, re);
vs.add(z);
}
List<CountEntry<Integer>> top = vs.peek(5);
System.out.println("Zipfian:");
for (CountEntry<Integer> e : top)
{
System.out.println(e);
}
CountEntry<Integer> tippyTop = top.get(0);
assertTrue(tippyTop.getItem() < 3);
}
示例9: testGeometricDistribution
import cern.jet.random.engine.RandomEngine; //导入依赖的package包/类
@Test
public void testGeometricDistribution()
{
RandomEngine re = RandomEngine.makeDefault();
for (int i = 0; i < NUM_ITERATIONS; i++)
{
int z = Distributions.nextGeometric(0.25, re);
vs.add(z);
}
List<CountEntry<Integer>> top = vs.peek(5);
System.out.println("Geometric:");
for (CountEntry<Integer> e : top)
{
System.out.println(e);
}
CountEntry<Integer> tippyTop = top.get(0);
assertTrue(tippyTop.getItem() < 3);
}
示例10: nextTriangular
import cern.jet.random.engine.RandomEngine; //导入依赖的package包/类
/**
* Returns a random number from the standard Triangular distribution in (-1,1).
* <p>
* <b>Implementation:</b> Inversion method.
* This is a port of <tt>tra.c</tt> from the <A HREF="http://www.cis.tu-graz.ac.at/stat/stadl/random.html">C-RAND / WIN-RAND</A> library.
* <p>
*/
public static double nextTriangular(RandomEngine randomGenerator) {
/******************************************************************
* *
* Triangular Distribution - Inversion: x = +-(1-sqrt(u)) *
* *
******************************************************************
* *
* FUNCTION : - tra samples a random number from the *
* standard Triangular distribution in (-1,1) *
* SUBPROGRAM : - drand(seed) ... (0,1)-Uniform generator with *
* unsigned long integer *seed. *
* *
******************************************************************/
double u;
u=randomGenerator.raw();
if (u<=0.5) return(Math.sqrt(2.0*u)-1.0); /* -1 <= x <= 0 */
else return(1.0-Math.sqrt(2.0*(1.0-u))); /* 0 <= x <= 1 */
}
示例11: nextZipfInt
import cern.jet.random.engine.RandomEngine; //导入依赖的package包/类
/**
* Returns a zipfian distributed random number with the given skew.
* <p>
* Algorithm from page 551 of:
* Devroye, Luc (1986) `Non-uniform random variate generation',
* Springer-Verlag: Berlin. ISBN 3-540-96305-7 (also 0-387-96305-7)
*
* @param z the skew of the distribution (must be >1.0).
* @returns a zipfian distributed number in the closed interval <tt>[1,Integer.MAX_VALUE]</tt>.
*/
public static int nextZipfInt(double z, RandomEngine randomGenerator) {
/* Algorithm from page 551 of:
* Devroye, Luc (1986) `Non-uniform random variate generation',
* Springer-Verlag: Berlin. ISBN 3-540-96305-7 (also 0-387-96305-7)
*/
final double b = Math.pow(2.0,z-1.0);
final double constant = -1.0/(z-1.0);
int result=0;
for (;;) {
double u = randomGenerator.raw();
double v = randomGenerator.raw();
result = (int) (Math.floor(Math.pow(u,constant)));
double t = Math.pow(1.0 + 1.0/result, z-1.0);
if (v*result*(t-1.0)/(b-1.0) <= t/b) break;
}
return result;
}
示例12: demo1
import cern.jet.random.engine.RandomEngine; //导入依赖的package包/类
/**
* Prints the first <tt>size</tt> random numbers generated by the distribution.
*/
public static void demo1() {
// Gamma distribution
// define distribution parameters
double mean = 5;
double variance = 1.5;
double alpha = mean*mean / variance;
double lambda = 1 / (variance / mean);
// for tests and debugging use a random engine with CONSTANT seed --> deterministic and reproducible results
cern.jet.random.engine.RandomEngine engine = new cern.jet.random.engine.MersenneTwister();
// your favourite distribution goes here
cern.jet.random.AbstractDistribution dist = new cern.jet.random.Gamma(alpha,lambda,engine);
// collect random numbers and print statistics
int size = 100000;
cern.colt.list.DoubleArrayList numbers = new cern.colt.list.DoubleArrayList(size);
for (int i=0; i < size; i++) numbers.add(dist.nextDouble());
hep.aida.bin.DynamicBin1D bin = new hep.aida.bin.DynamicBin1D();
bin.addAllOf(numbers);
System.out.println(bin);
}
示例13: sample
import cern.jet.random.engine.RandomEngine; //导入依赖的package包/类
/**
* Efficiently computes a sorted random set of <tt>count</tt> elements from the interval <tt>[low,low+N-1]</tt>.
* Since we are talking about a random set, no element will occur more than once.
*
* <p>Running time is <tt>O(count)</tt>, on average. Space requirements are zero.
*
* <p>Numbers are filled into the specified array starting at index <tt>fromIndex</tt> to the right.
* The array is returned sorted ascending in the range filled with numbers.
*
* <p><b>Random number generation:</b> By default uses <tt>MersenneTwister</tt>, a very strong random number generator, much better than <tt>java.util.Random</tt>.
* You can also use other strong random number generators of Paul Houle's RngPack package.
* For example, <tt>Ranecu</tt>, <tt>Ranmar</tt> and <tt>Ranlux</tt> are strong well analyzed research grade pseudo-random number generators with known periods.
*
* @param n the total number of elements to choose (must be <tt>n >= 0</tt> and <tt>n <= N</tt>).
* @param N the interval to choose random numbers from is <tt>[low,low+N-1]</tt>.
* @param count the number of elements to be filled into <tt>values</tt> by this call (must be >= 0 and <=<tt>n</tt>). Normally, you will set <tt>count=n</tt>.
* @param low the interval to choose random numbers from is <tt>[low,low+N-1]</tt>. Hint: If <tt>low==0</tt>, then draws random numbers from the interval <tt>[0,N-1]</tt>.
* @param values the array into which the random numbers are to be filled; must have a length <tt>>= count+fromIndex</tt>.
* @param fromIndex the first index within <tt>values</tt> to be filled with numbers (inclusive).
* @param randomGenerator a random number generator. Set this parameter to <tt>null</tt> to use the default random number generator.
*/
public static void sample(long n, long N, int count, long low, long[] values, int fromIndex, RandomEngine randomGenerator) {
if (n<=0 || count<=0) return;
if (count>n) throw new IllegalArgumentException("count must not be greater than n");
if (randomGenerator==null) randomGenerator = cern.jet.random.AbstractDistribution.makeDefaultGenerator();
if (count==N) { // rare case treated quickly
long val = low;
int limit= fromIndex+count;
for (int i=fromIndex; i<limit; ) values[i++] = val++;
return;
}
if (n<N*0.95) { // || Math.min(count,N-n)>maxTmpMemoryAllowed) {
sampleMethodD(n,N,count,low,values,fromIndex,randomGenerator);
}
else { // More than 95% of all numbers shall be sampled.
rejectMethodD(n,N,count,low,values,fromIndex,randomGenerator);
}
}
示例14: viewSample
import cern.jet.random.engine.RandomEngine; //导入依赖的package包/类
/**
Constructs and returns a sampling view with a size of <tt>round(matrix.size() * fraction)</tt>.
Samples "without replacement" from the uniform distribution.
@param matrix any matrix.
@param rowFraction the percentage of rows to be included in the view.
@param columnFraction the percentage of columns to be included in the view.
@param randomGenerator a uniform random number generator; set this parameter to <tt>null</tt> to use a default generator seeded with the current time.
@return the sampling view.
@throws IllegalArgumentException if <tt>! (0 <= rowFraction <= 1 && 0 <= columnFraction <= 1)</tt>.
@see cern.jet.random.sampling.RandomSampler
*/
public static DoubleMatrix1D viewSample(DoubleMatrix1D matrix, double fraction, RandomEngine randomGenerator) {
// check preconditions and allow for a little tolerance
double epsilon = 1e-09;
if (fraction < 0 - epsilon || fraction > 1 + epsilon) throw new IllegalArgumentException();
if (fraction < 0) fraction = 0;
if (fraction > 1) fraction = 1;
// random generator seeded with current time
if (randomGenerator==null) randomGenerator = new cern.jet.random.engine.MersenneTwister((int) System.currentTimeMillis());
int ncols = (int) Math.round(matrix.size() * fraction);
int max = ncols;
long[] selected = new long[max]; // sampler works on long's, not int's
// sample
int n = ncols;
int N = matrix.size();
cern.jet.random.sampling.RandomSampler.sample(n,N,n,0,selected,0,randomGenerator);
int[] selectedCols = new int[n];
for (int i=0; i<n; i++) selectedCols[i] = (int) selected[i];
return matrix.viewSelection(selectedCols);
}
示例15: testCorrectArmChosen
import cern.jet.random.engine.RandomEngine; //导入依赖的package包/类
@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);
}