本文整理匯總了Java中org.apache.commons.math3.random.RandomGenerator.nextDouble方法的典型用法代碼示例。如果您正苦於以下問題:Java RandomGenerator.nextDouble方法的具體用法?Java RandomGenerator.nextDouble怎麽用?Java RandomGenerator.nextDouble使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.commons.math3.random.RandomGenerator
的用法示例。
在下文中一共展示了RandomGenerator.nextDouble方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: inputBroadcast
import org.apache.commons.math3.random.RandomGenerator; //導入方法依賴的package包/類
public void inputBroadcast( RandomGenerator r ,
CarmaSystem system,
CarmaComponent sender,
int action,
CarmaPredicate predicate,
Object value ) {
if (predicate.satisfy(system.now(),store)) {
WeightedStructure<Activity> enabledInputs = new ComposedWeightedStructure<Activity>();
double missingProbability = 1.0;
for (CarmaProcess p : processes) {//TODO: fix this!
WeightedStructure<Activity> foo = p.doReceiveBroadcast(system, sender.store , action, value );
missingProbability = missingProbability*(1-foo.getTotalWeight());
enabledInputs = enabledInputs.add(foo);
}
double select = r.nextDouble()*(enabledInputs.getTotalWeight()+missingProbability);
if (select<enabledInputs.getTotalWeight()) {
enabledInputs.select(select).getElement().execute(r);
}
}
}
示例2: testRandom
import org.apache.commons.math3.random.RandomGenerator; //導入方法依賴的package包/類
@Test
public void testRandom() {
final RandomGenerator random = new Well1024a(0xd015982e9f31ee04l);
final UnitSphereRandomVectorGenerator sr = new UnitSphereRandomVectorGenerator(3, random);
for (int i = 0; i < 100; ++i) {
double d = 25 * random.nextDouble();
double refRadius = 10 * random.nextDouble();
Vector3D refCenter = new Vector3D(d, new Vector3D(sr.nextVector()));
List<Vector3D> support = new ArrayList<Vector3D>();
for (int j = 0; j < 5; ++j) {
support.add(new Vector3D(1.0, refCenter, refRadius, new Vector3D(sr.nextVector())));
}
EnclosingBall<Euclidean3D, Vector3D> sphere = new SphereGenerator().ballOnSupport(support);
Assert.assertEquals(0.0, refCenter.distance(sphere.getCenter()), 4e-7 * refRadius);
Assert.assertEquals(refRadius, sphere.getRadius(), 1e-7 * refRadius);
}
}
示例3: testRandom
import org.apache.commons.math3.random.RandomGenerator; //導入方法依賴的package包/類
@Test
public void testRandom() {
final RandomGenerator random = new Well1024a(0x12faa818373ffe90l);
final UnitSphereRandomVectorGenerator sr = new UnitSphereRandomVectorGenerator(2, random);
for (int i = 0; i < 500; ++i) {
double d = 25 * random.nextDouble();
double refRadius = 10 * random.nextDouble();
Vector2D refCenter = new Vector2D(d, new Vector2D(sr.nextVector()));
List<Vector2D> support = new ArrayList<Vector2D>();
for (int j = 0; j < 3; ++j) {
support.add(new Vector2D(1.0, refCenter, refRadius, new Vector2D(sr.nextVector())));
}
EnclosingBall<Euclidean2D, Vector2D> disk = new DiskGenerator().ballOnSupport(support);
Assert.assertEquals(0.0, refCenter.distance(disk.getCenter()), 3e-9 * refRadius);
Assert.assertEquals(refRadius, disk.getRadius(), 7e-10 * refRadius);
}
}
示例4: generateAllelicCount
import org.apache.commons.math3.random.RandomGenerator; //導入方法依賴的package包/類
protected static AllelicCount generateAllelicCount(final double minorFraction, final SimpleInterval position,
final RandomGenerator rng, final GammaDistribution biasGenerator, final double outlierProbability) {
final int numReads = 100;
final double bias = biasGenerator.sample();
//flip a coin to decide alt minor (alt fraction = minor fraction) or ref minor (alt fraction = 1 - minor fraction)
final double altFraction = rng.nextDouble() < 0.5 ? minorFraction : 1 - minorFraction;
//the probability of an alt read is the alt fraction modified by the bias or, in the case of an outlier, random
final double pAlt = rng.nextDouble() < outlierProbability ? rng.nextDouble()
: altFraction / (altFraction + (1 - altFraction) * bias);
final int numAltReads = new BinomialDistribution(rng, numReads, pAlt).sample();
final int numRefReads = numReads - numAltReads;
return new AllelicCount(position, numAltReads, numRefReads);
}
示例5: chooseEndpoint
import org.apache.commons.math3.random.RandomGenerator; //導入方法依賴的package包/類
Endpoint chooseEndpoint(RandomGenerator random) {
double p = random.nextDouble();
int i = 0;
while (i < cumulativeProbs.length && p >= cumulativeProbs[i]) {
i++;
}
return endpoints[i];
}
示例6: generate
import org.apache.commons.math3.random.RandomGenerator; //導入方法依賴的package包/類
@Override
public Pair<String,String> generate(int id, RandomGenerator random) {
List<String> elements = new ArrayList<>(numberOfDimensions);
for (int i = 0; i < numberOfDimensions; i++) {
double d = random.nextDouble();
elements.add(Double.toString(d));
}
return new Pair<>(Integer.toString(id), TextUtils.joinDelimited(elements, ','));
}
示例7: generate
import org.apache.commons.math3.random.RandomGenerator; //導入方法依賴的package包/類
@Override
public Pair<String,String> generate(int id, RandomGenerator random) {
List<String> elements = new ArrayList<>(n + 2);
elements.add(Integer.toString(id));
boolean positive = true;
for (int i = 0; i < n; i++) {
double d = random.nextDouble();
if (d < 0.5) {
positive = false;
}
elements.add(Double.toString(d));
}
elements.add(Boolean.toString(positive));
return new Pair<>(Integer.toString(id), TextUtils.joinDelimited(elements, ','));
}
示例8: generate
import org.apache.commons.math3.random.RandomGenerator; //導入方法依賴的package包/類
@Override
public Pair<String, String> generate(int id, RandomGenerator random) {
boolean positive = id % 2 != 0;
String target = positive ? "banana" : "apple";
// 10% chance of wrong predictor
String predictor = (positive ^ (random.nextDouble() < 0.1)) ? "yellow" : "red";
return new Pair<>(null, predictor + "," + target);
}
示例9: testRandomState
import org.apache.commons.math3.random.RandomGenerator; //導入方法依賴的package包/類
@Ignore
@Test
public void testRandomState() {
RandomGenerator generator = RandomManager.getRandom();
double unseededValue = generator.nextDouble();
RandomManager.useTestSeed();
double seededValue = generator.nextDouble();
assertNotEquals(unseededValue, seededValue);
assertEquals(seededValue, RandomManager.getRandom().nextDouble());
}
示例10: binomial
import org.apache.commons.math3.random.RandomGenerator; //導入方法依賴的package包/類
/**
* Generates a binomial distributed number using
* the given rng
*
* @param rng
* @param n
* @param p
* @return
*/
public static int binomial(RandomGenerator rng, int n, double p) {
if ((p < 0) || (p > 1)) {
return 0;
}
int c = 0;
for (int i = 0; i < n; i++) {
if (rng.nextDouble() < p) {
c++;
}
}
return c;
}
示例11: mate
import org.apache.commons.math3.random.RandomGenerator; //導入方法依賴的package包/類
/**
* Helper for {@link #crossover(Chromosome, Chromosome)}. Performs the actual crossover.
*
* @param first the first chromosome
* @param second the second chromosome
* @return the pair of new chromosomes that resulted from the crossover
* @throws DimensionMismatchException if the length of the two chromosomes is different
*/
private ChromosomePair mate(final AbstractListChromosome<T> first,
final AbstractListChromosome<T> second) throws DimensionMismatchException {
final int length = first.getLength();
if (length != second.getLength()) {
throw new DimensionMismatchException(second.getLength(), length);
}
// array representations of the parents
final List<T> parent1Rep = first.getRepresentation();
final List<T> parent2Rep = second.getRepresentation();
// and of the children
final List<T> child1Rep = new ArrayList<T>(length);
final List<T> child2Rep = new ArrayList<T>(length);
final RandomGenerator random = GeneticAlgorithm.getRandomGenerator();
for (int index = 0; index < length; index++) {
if (random.nextDouble() < ratio) {
// swap the bits -> take other parent
child1Rep.add(parent2Rep.get(index));
child2Rep.add(parent1Rep.get(index));
} else {
child1Rep.add(parent1Rep.get(index));
child2Rep.add(parent2Rep.get(index));
}
}
return new ChromosomePair(first.newFixedLengthChromosome(child1Rep),
second.newFixedLengthChromosome(child2Rep));
}
示例12: algorithmBB
import org.apache.commons.math3.random.RandomGenerator; //導入方法依賴的package包/類
/**
* Returns one sample using Cheng's BB algorithm, when both α and β are greater than 1.
* @param random random generator to use
* @param a0 distribution first shape parameter (α)
* @param a min(α, β) where α, β are the two distribution shape parameters
* @param b max(α, β) where α, β are the two distribution shape parameters
* @return sampled value
*/
private static double algorithmBB(RandomGenerator random,
final double a0,
final double a,
final double b) {
final double alpha = a + b;
final double beta = FastMath.sqrt((alpha - 2.) / (2. * a * b - alpha));
final double gamma = a + 1. / beta;
double r;
double w;
double t;
do {
final double u1 = random.nextDouble();
final double u2 = random.nextDouble();
final double v = beta * (FastMath.log(u1) - FastMath.log1p(-u1));
w = a * FastMath.exp(v);
final double z = u1 * u1 * u2;
r = gamma * v - 1.3862944;
final double s = a + r - w;
if (s + 2.609438 >= 5 * z) {
break;
}
t = FastMath.log(z);
if (s >= t) {
break;
}
} while (r + alpha * (FastMath.log(alpha) - FastMath.log(b + w)) < t);
w = FastMath.min(w, Double.MAX_VALUE);
return Precision.equals(a, a0) ? w / (b + w) : b / (b + w);
}
示例13: sample
import org.apache.commons.math3.random.RandomGenerator; //導入方法依賴的package包/類
public double sample(final RandomGenerator rng, final Function<Double, Double> logPDF) {
Utils.nonNull(rng);
Utils.nonNull(logPDF);
final AbstractRealDistribution normal = new NormalDistribution(rng, 0, 1);
final double proposal = xCurrent + stepSize * normal.sample();
final double acceptanceProbability = (proposal < lowerBound || upperBound < proposal) ? 0
: Math.min(1, Math.exp(logPDF.apply(proposal) - logPDF.apply(xCurrent)));
//adjust stepSize larger/smaller to decrease/increase the acceptance rate
final double correctionFactor = (acceptanceProbability - optimalAcceptanceRate) * adjustmentRate * (timeScale / (timeScale + iteration));
stepSize *= Math.exp(correctionFactor);
iteration++;
return rng.nextDouble() < acceptanceProbability ? proposal : xCurrent;
}
示例14: makeBlobs
import org.apache.commons.math3.random.RandomGenerator; //導入方法依賴的package包/類
public static List<Vector2D> makeBlobs(int samples, int centers, double clusterStd,
double min, double max, boolean shuffle, RandomGenerator random) {
NormalDistribution dist = new NormalDistribution(random, 0.0, clusterStd, 1e-9);
double range = max - min;
Vector2D[] centerPoints = new Vector2D[centers];
for (int i = 0; i < centers; i++) {
double x = random.nextDouble() * range + min;
double y = random.nextDouble() * range + min;
centerPoints[i] = new Vector2D(x, y);
}
int[] nSamplesPerCenter = new int[centers];
int count = samples / centers;
Arrays.fill(nSamplesPerCenter, count);
for (int i = 0; i < samples % centers; i++) {
nSamplesPerCenter[i]++;
}
List<Vector2D> points = new ArrayList<Vector2D>();
for (int i = 0; i < centers; i++) {
for (int j = 0; j < nSamplesPerCenter[i]; j++) {
Vector2D point = new Vector2D(dist.sample(), dist.sample());
points.add(point.add(centerPoints[i]));
}
}
if (shuffle) {
Collections.shuffle(points, new RandomAdaptor(random));
}
return points;
}
示例15: testTransform
import org.apache.commons.math3.random.RandomGenerator; //導入方法依賴的package包/類
@Test
public void testTransform() {
RandomGenerator random = new Well1024a(0x16992fc4294bf2f1l);
UnitSphereRandomVectorGenerator sphRandom = new UnitSphereRandomVectorGenerator(3, random);
for (int i = 0; i < 100; ++i) {
Rotation r = new Rotation(new Vector3D(sphRandom.nextVector()),
FastMath.PI * random.nextDouble());
Transform<Sphere2D, Sphere1D> t = Circle.getTransform(r);
S2Point p = new S2Point(new Vector3D(sphRandom.nextVector()));
S2Point tp = (S2Point) t.apply(p);
Assert.assertEquals(0.0, r.applyTo(p.getVector()).distance(tp.getVector()), 1.0e-10);
Circle c = new Circle(new Vector3D(sphRandom.nextVector()), 1.0e-10);
Circle tc = (Circle) t.apply(c);
Assert.assertEquals(0.0, r.applyTo(c.getPole()).distance(tc.getPole()), 1.0e-10);
Assert.assertEquals(0.0, r.applyTo(c.getXAxis()).distance(tc.getXAxis()), 1.0e-10);
Assert.assertEquals(0.0, r.applyTo(c.getYAxis()).distance(tc.getYAxis()), 1.0e-10);
Assert.assertEquals(c.getTolerance(), ((Circle) t.apply(c)).getTolerance(), 1.0e-10);
SubLimitAngle sub = new LimitAngle(new S1Point(MathUtils.TWO_PI * random.nextDouble()),
random.nextBoolean(), 1.0e-10).wholeHyperplane();
Vector3D psub = c.getPointAt(((LimitAngle) sub.getHyperplane()).getLocation().getAlpha());
SubLimitAngle tsub = (SubLimitAngle) t.apply(sub, c, tc);
Vector3D ptsub = tc.getPointAt(((LimitAngle) tsub.getHyperplane()).getLocation().getAlpha());
Assert.assertEquals(0.0, r.applyTo(psub).distance(ptsub), 1.0e-10);
}
}