本文整理匯總了Java中org.apache.commons.math3.random.RandomDataGenerator.nextUniform方法的典型用法代碼示例。如果您正苦於以下問題:Java RandomDataGenerator.nextUniform方法的具體用法?Java RandomDataGenerator.nextUniform怎麽用?Java RandomDataGenerator.nextUniform使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.commons.math3.random.RandomDataGenerator
的用法示例。
在下文中一共展示了RandomDataGenerator.nextUniform方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: addRandomTile
import org.apache.commons.math3.random.RandomDataGenerator; //導入方法依賴的package包/類
public void addRandomTile(RandomDataGenerator random) {
List<Integer> emptyLocations = new ArrayList<>();
for (int location = 0; location < SIZE * SIZE; location++) {
if (getValue(location) == 0) {
emptyLocations.add(location);
}
}
Integer randomEmptyLocation = RandomUtils.pickRandom(emptyLocations, random);
boolean isFour = (random.nextUniform(0, 1, true) < RANDOM_FOUR_PROB);
if (isFour) {
setValue(randomEmptyLocation, 2);
} else {
setValue(randomEmptyLocation, 1);
}
}
示例2: randomNormal
import org.apache.commons.math3.random.RandomDataGenerator; //導入方法依賴的package包/類
public static double randomNormal(RandomDataGenerator rnd, final double min, final double max, final double variance) {
double value;
if (min == max)
return min;
if (variance == 0.0) {
value = rnd.nextUniform(min, max);
} else {
do {
value = rnd.nextGaussian((max + min)/2, variance);
} while (value < min || value > max);
}
return value;
}
示例3: sampleDataset
import org.apache.commons.math3.random.RandomDataGenerator; //導入方法依賴的package包/類
private static XYDataset sampleDataset() {
RandomDataGenerator rnd = new RandomDataGenerator();
XYSeries series1 = new XYSeries("First");
XYSeries series2 = new XYSeries("Second");
XYSeries series3 = new XYSeries("Third");
for (int input = 100; input <= 1000; input+=100) {
double value = rnd.nextUniform(100, 1000000);
series1.add(input, value);
series2.add(input, value * 10);
series3.add(input, value * 100);
}
XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(series1);
dataset.addSeries(series2);
dataset.addSeries(series3);
return dataset;
}
示例4: getRandomValue
import org.apache.commons.math3.random.RandomDataGenerator; //導入方法依賴的package包/類
/**
* @param rdg random number generator to use
* @return a hyperparameter value chosen uniformly at random from the range
*/
@Override
public Double getRandomValue(RandomDataGenerator rdg) {
if (max == min) {
return min;
}
return rdg.nextUniform(min, max, true);
}
示例5: testExpectedValues
import org.apache.commons.math3.random.RandomDataGenerator; //導入方法依賴的package包/類
@Test
public void testExpectedValues() {
long seed = 0;
Random rand = new Random (seed);
RandomDataGenerator randomData = new RandomDataGenerator();
double uniformAverage = 0.0;
double binomialAverage = 0.0;
double normalAverage = 0.0;
double exponentialAverage = 0.0;
for(int i=0; i<1e6; i++){
// set current seed
randomData.reSeed(seed);
// draw random numbers
double n = randomData.nextGaussian(0, 1);
double u = randomData.nextUniform(0, 1);
double b = randomData.nextBinomial(1, 0.5);
double e = randomData.nextExponential(0.25);
// average mean random number
uniformAverage += (1. / (1.+i))*(u - uniformAverage);
binomialAverage += (1. / (1.+i))*(b - binomialAverage);
normalAverage += (1. / (1.+i))*(n - normalAverage);
exponentialAverage += (1. / (1.+i))*(e - exponentialAverage);
// draw new seed from global random generator
seed = rand.nextLong();
}
assertEquals (0.5, uniformAverage, 0.001);
assertEquals (0.5, binomialAverage, 0.001);
assertEquals (0.0, normalAverage, 0.001);
assertEquals (0.25, exponentialAverage, 0.001);
}
示例6: uniform
import org.apache.commons.math3.random.RandomDataGenerator; //導入方法依賴的package包/類
@Override
public Double uniform( RandomDataGenerator random, Double min, Double max )
{
if ( min == max )
{
return min;
}
return random.nextUniform( min, max, true );
}
示例7: nextUniform
import org.apache.commons.math3.random.RandomDataGenerator; //導入方法依賴的package包/類
/** Same as random.nextDouble(lower, upper, true), but does not throw exception when lower == upper */
public static double nextUniform(double lower, double upper, RandomDataGenerator random) {
return lower == upper ? lower : random.nextUniform(lower, upper, true);
}
示例8: generate
import org.apache.commons.math3.random.RandomDataGenerator; //導入方法依賴的package包/類
public void generate(int sleep_frequency) throws InterruptedException {
ThroughputLog throughput = new ThroughputLog(this.getClass().getSimpleName());
long time = System.currentTimeMillis();
// Obtain a cached thread pool
ExecutorService cachedPool = Executors.newCachedThreadPool();
RandomDataGenerator generator = new RandomDataGenerator();
generator.reSeed(10000000L);
// sub thread use variable in main thread
// for loop to generate advertisement
ArrayList<Advertisement> advList = new ArrayList<>();
for (long i = 1; i < ADV_NUM; ++i) {
// advertisement id
String advId = UUID.randomUUID().toString();
long timestamp = System.currentTimeMillis();
producer.send(new ProducerRecord<>(ADV_TOPIC, advId, String.format("%d\t%s", timestamp, advId)));
// System.out.println("Shown: " + System.currentTimeMillis() + "\t" + advId);
// whether customer clicked this advertisement
if (generator.nextUniform(0, 1) <= clickProbability) {
// long deltaT = (long)generator.nextExponential(clickLambda)*1000;
long deltaT = (long) generator.nextGaussian(clickLambda, 1) * 1000;
// System.out.println(deltaT);
advList.add(new Advertisement(advId, timestamp + deltaT));
}
if (i % 100 == 0) {
cachedPool.submit(new ClickThread(advList));
advList = new ArrayList<>();
}
throughput.execute();
// control data generate speed
if (sleep_frequency > 0 && i % sleep_frequency == 0) {
Thread.sleep(1);
}
}
cachedPool.shutdown();
try {
cachedPool.awaitTermination(1, TimeUnit.MINUTES);
} catch (InterruptedException e) {
}
logger.info("LatencyLog: " + String.valueOf(System.currentTimeMillis() - time));
}
示例9: getRandomInRange
import org.apache.commons.math3.random.RandomDataGenerator; //導入方法依賴的package包/類
private double getRandomInRange(double min, double max) {
RandomDataGenerator generator = new RandomDataGenerator();
return generator.nextUniform(min, max);
}
示例10: showSimulationDialog
import org.apache.commons.math3.random.RandomDataGenerator; //導入方法依賴的package包/類
private boolean showSimulationDialog()
{
ExtendedGenericDialog gd = new ExtendedGenericDialog(TITLE);
SimulationDistribution[] distributionValues = SimulationDistribution.values();
String[] distribution = SettingsManager.getNames((Object[]) distributionValues);
// Random crosstalk if not set
if (Maths.max(ct) == 0)
{
RandomDataGenerator rdg = getRandomDataGenerator();
for (int i = 0; i < ct.length; i++)
ct[i] = rdg.nextUniform(0.05, 0.15); // Have some crosstalk
}
// Three channel
for (int c = 0; c < 3; c++)
{
String ch = "_C" + (c + 1);
gd.addNumericField("Molcules" + ch, sim_nMolecules[c], 0);
gd.addChoice("Distribution" + ch, distribution, distribution[sim_distribution[c].ordinal()]);
gd.addNumericField("Precision_" + ch, sim_precision[c], 3);
gd.addNumericField("Crosstalk_" + ctNames[2 * c], ct[2 * c], 3);
gd.addNumericField("Crosstalk_" + ctNames[2 * c + 1], ct[2 * c + 1], 3);
}
gd.showDialog();
if (gd.wasCanceled())
return false;
int count = 0;
for (int c = 0; c < 3; c++)
{
sim_nMolecules[c] = (int) Math.abs(gd.getNextNumber());
if (sim_nMolecules[c] > 0)
count++;
sim_distribution[c] = distributionValues[gd.getNextChoiceIndex()];
sim_precision[c] = Math.abs(gd.getNextNumber());
ct[2 * c] = Math.abs(gd.getNextNumber());
ct[2 * c + 1] = Math.abs(gd.getNextNumber());
}
if (gd.invalidNumber())
return false;
if (count < 2)
{
IJ.error(TITLE, "Simulation requires at least 2 channels");
return false;
}
try
{
for (int i = 0; i < ct.length; i += 2)
{
if (sim_nMolecules[i / 2] > 0)
validateCrosstalk(i, i + 1);
}
}
catch (IllegalArgumentException ex)
{
IJ.error(TITLE, ex.getMessage());
return false;
}
return true;
}
示例11: crlbIsHigherWithPrecomputed
import org.apache.commons.math3.random.RandomDataGenerator; //導入方法依賴的package包/類
@Test
public void crlbIsHigherWithPrecomputed()
{
int iter = 10;
rdg = new RandomDataGenerator(new Well19937c(30051977));
ErfGaussian2DFunction func = (ErfGaussian2DFunction) GaussianFunctionFactory.create2D(1, 10, 10,
GaussianFunctionFactory.FIT_ERF_FREE_CIRCLE, null);
double[] a = new double[1 + Gaussian2DFunction.PARAMETERS_PER_PEAK];
int n = func.getNumberOfGradients();
// Get a background
double[] b = new double[func.size()];
for (int i = 0; i < b.length; i++)
b[i] = rdg.nextUniform(1, 2);
for (int i = 0; i < iter; i++)
{
a[Gaussian2DFunction.BACKGROUND] = rdg.nextUniform(0.1, 0.3);
a[Gaussian2DFunction.SIGNAL] = rdg.nextUniform(100, 300);
a[Gaussian2DFunction.X_POSITION] = rdg.nextUniform(4, 6);
a[Gaussian2DFunction.Y_POSITION] = rdg.nextUniform(4, 6);
a[Gaussian2DFunction.X_SD] = rdg.nextUniform(1, 1.3);
a[Gaussian2DFunction.Y_SD] = rdg.nextUniform(1, 1.3);
PoissonGradientProcedure p1 = PoissonGradientProcedureFactory.create(func);
p1.computeFisherInformation(a);
PoissonGradientProcedure p2 = PoissonGradientProcedureFactory
.create(PrecomputedGradient1Function.wrapGradient1Function(func, b));
p2.computeFisherInformation(a);
FisherInformationMatrix m1 = new FisherInformationMatrix(p1.getLinear(), n);
FisherInformationMatrix m2 = new FisherInformationMatrix(p2.getLinear(), n);
double[] crlb1 = m1.crlb();
double[] crlb2 = m2.crlb();
Assert.assertNotNull(crlb1);
Assert.assertNotNull(crlb2);
//System.out.printf("%s : %s\n", Arrays.toString(crlb1), Arrays.toString(crlb2));
for (int j = 0; j < n; j++)
Assert.assertTrue(crlb1[j] < crlb2[j]);
}
}
示例12: crlbIsHigherWithPrecomputed
import org.apache.commons.math3.random.RandomDataGenerator; //導入方法依賴的package包/類
@Test
public void crlbIsHigherWithPrecomputed()
{
int iter = 10;
rdg = new RandomDataGenerator(new Well19937c(30051977));
ErfGaussian2DFunction func = (ErfGaussian2DFunction) GaussianFunctionFactory.create2D(1, 10, 10,
GaussianFunctionFactory.FIT_ERF_FREE_CIRCLE, null);
double[] a = new double[1 + Gaussian2DFunction.PARAMETERS_PER_PEAK];
int n = func.getNumberOfGradients();
// Get a background
double[] b = new double[func.size()];
for (int i = 0; i < b.length; i++)
b[i] = rdg.nextUniform(1, 2);
for (int i = 0; i < iter; i++)
{
a[Gaussian2DFunction.BACKGROUND] = rdg.nextUniform(0.1, 0.3);
a[Gaussian2DFunction.SIGNAL] = rdg.nextUniform(100, 300);
a[Gaussian2DFunction.X_POSITION] = rdg.nextUniform(4, 6);
a[Gaussian2DFunction.Y_POSITION] = rdg.nextUniform(4, 6);
a[Gaussian2DFunction.X_SD] = rdg.nextUniform(1, 1.3);
a[Gaussian2DFunction.Y_SD] = rdg.nextUniform(1, 1.3);
LSQVarianceGradientProcedure p1 = LSQVarianceGradientProcedureFactory.create(func);
p1.variance(a);
LSQVarianceGradientProcedure p2 = LSQVarianceGradientProcedureFactory
.create(PrecomputedGradient1Function.wrapGradient1Function(func, b));
p2.variance(a);
double[] crlb1 = p1.variance;
double[] crlb2 = p2.variance;
Assert.assertNotNull(crlb1);
Assert.assertNotNull(crlb2);
//System.out.printf("%s : %s\n", Arrays.toString(crlb1), Arrays.toString(crlb2));
for (int j = 0; j < n; j++)
Assert.assertTrue(crlb1[j] < crlb2[j]);
}
}
示例13: genCrack2d
import org.apache.commons.math3.random.RandomDataGenerator; //導入方法依賴的package包/類
public void genCrack2d( float[] startPt, float[] endPt, int crackLength,
float distShp, float distScale, float skewMn, float skewVar,
RandomDataGenerator rand ){
int ndims = startPt.length;
crackCtrlPts = new float[crackLength][ndims];
crackCtrlOffsets = new float[crackLength][ndims];
crackCtrlPts[ 0 ] = startPt;
crackCtrlPts[ crackLength-1 ] = endPt;
double[] ptSpacing = new double[ crackLength - 2 ];
for( int i=0; i<ptSpacing.length; i++){
if( rand != null ){
ptSpacing[i] = rand.nextUniform( 0, 1 );
}else{
ptSpacing[i] = i / (double)(ptSpacing.length-1) ;
}
}
Arrays.sort( ptSpacing );
logger.debug(" pt Spacing \n" + ArrayUtil.printArray( ptSpacing ));
for( int i=0; i<ptSpacing.length; i++){
for( int d=0; d<startPt.length; d++){
crackCtrlPts[ i+1 ][d] = (float)((1 - ptSpacing[i]) * startPt[d] + ptSpacing[i] * endPt[d]);
}
}
float[] lineVec = ArrayUtil.subtract( endPt, startPt );
ArrayUtil.normalizeLengthInPlace(lineVec);
float[] lineVecPerp = SimpleLinAlg.orth2d( lineVec );
double skewAmt = 0;
double dist = distShp;
for( int i=0; i<crackLength; i++)
{
if ( rand != null ){
skewAmt = rand.nextGaussian( skewMn, skewVar);
dist = rand.nextGamma( distShp, distScale );
}
for( int d=0; d<startPt.length; d++)
{
crackCtrlOffsets[i][d] = (float)( lineVec[d] * skewAmt + lineVecPerp[d] * ( 1 - skewAmt ) );
crackCtrlOffsets[i][d] *= dist;
}
}
logger.debug(" crack Pts:\n" + ArrayUtil.printArray( crackCtrlPts ));
logger.debug("\n crack Offsets:\n" + ArrayUtil.printArray( crackCtrlOffsets ));
buildXfmMesh();
}
示例14: nextRandomDouble
import org.apache.commons.math3.random.RandomDataGenerator; //導入方法依賴的package包/類
/**
* Returns a uniformly distributed random double value in the opened
* interval ({@code lower}, {@code upper}).
*
* @param rng
* the random number generator used to generate the value
* @param lower
* lower bound
* @param upper
* upper bound
*
* @return random double value between {@code lower} and {@code upper}
*
* @throws IllegalStateException
* if {@code lower} is not smaller than {@code upper}
*
*/
public static double nextRandomDouble(Random rng, double lower,
double upper) {
if (lower >= upper)
throw new IllegalStateException("lower must be smaller then upper");
RandomDataGenerator rndGenerator = new RandomDataGenerator(
RandomGeneratorFactory.createRandomGenerator(rng));
return rndGenerator.nextUniform(lower, upper);
}