本文整理匯總了Java中org.apache.commons.lang.math.RandomUtils.nextDouble方法的典型用法代碼示例。如果您正苦於以下問題:Java RandomUtils.nextDouble方法的具體用法?Java RandomUtils.nextDouble怎麽用?Java RandomUtils.nextDouble使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.commons.lang.math.RandomUtils
的用法示例。
在下文中一共展示了RandomUtils.nextDouble方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: runExperiment
import org.apache.commons.lang.math.RandomUtils; //導入方法依賴的package包/類
public void runExperiment() {
final ParallelThreadPool tp = new ParallelThreadPool();
for (final File f : allFiles) {
tp.pushTask(new Evaluator(f));
if (RandomUtils.nextDouble() < .02) {
tp.pushTask(new Runnable() {
@Override
public void run() {
result.print();
}
});
}
}
tp.waitForTermination();
result.print();
}
示例2: createParam
import org.apache.commons.lang.math.RandomUtils; //導入方法依賴的package包/類
private Object createParam(Class type) throws IllegalAccessException, InvocationTargetException,
InstantiationException {
Object param;
if (type == String.class) {
param = "test";
} else if (type == Integer.class || type == Integer.TYPE) {
param = RandomUtils.nextInt();
} else if (type == Long.class || type == Long.TYPE) {
param = RandomUtils.nextLong();
} else if (type == Float.class || type == Float.TYPE) {
param = RandomUtils.nextFloat();
} else if (type == Double.class || type == Double.TYPE) {
param = RandomUtils.nextDouble();
} else if (type == Boolean.class || type == Boolean.TYPE) {
param = RandomUtils.nextBoolean();
} else if (type == BigInteger.class) {
param = new BigInteger(TEST_BIGINTEGER);
} else if (type == List.class) {
param = new ArrayList<>();
} else if (type == XMLGregorianCalendar.class) {
try {
param = DatatypeFactory.newInstance().newXMLGregorianCalendar();
} catch (DatatypeConfigurationException e) {
throw new IllegalArgumentException(e.getMessage(), e);
}
} else {
Constructor[] constructors = type.getConstructors();
param = constructors[0].newInstance();
}
return param;
}
示例3: generate
import org.apache.commons.lang.math.RandomUtils; //導入方法依賴的package包/類
/**
* {@inheritDoc}
*/
public char[] generate(final int minimumLength, final int maximumLength) {
double d = RandomUtils.nextDouble();
int len = minimumLength
+ (int) (d * (double) (maximumLength - minimumLength));
char[] ret = new char[len];
for (int i = 0; i < len; i++) {
ret[i] = SOURCE[RandomUtils.nextInt(SOURCE_COUNT)];
}
return ret;
}
示例4: copyChildren
import org.apache.commons.lang.math.RandomUtils; //導入方法依賴的package包/類
/**
* Copy children
*
* @param toNode
* @param fromNode
* @param precentRootsToIntroduce
*/
public static void copyChildren(final TreeNode<TSGNode> toNode,
final TreeNode<Integer> fromNode,
final double precentRootsToIntroduce) {
final ArrayDeque<TreeNode<TSGNode>> toStack = new ArrayDeque<TreeNode<TSGNode>>();
final ArrayDeque<TreeNode<Integer>> fromStack = new ArrayDeque<TreeNode<Integer>>();
toStack.push(toNode);
fromStack.push(fromNode);
while (!toStack.isEmpty()) {
final TreeNode<TSGNode> currentTo = toStack.pop();
final TreeNode<Integer> currentFrom = fromStack.pop();
final List<List<TreeNode<Integer>>> children = currentFrom
.getChildrenByProperty();
for (int i = 0; i < children.size(); i++) {
for (final TreeNode<Integer> fromChild : children.get(i)) {
final TSGNode toChildData = new TSGNode(fromChild.getData());
if (fromChild.isLeaf()) {
toChildData.isRoot = false;
} else {
toChildData.isRoot = RandomUtils.nextDouble() < precentRootsToIntroduce;
}
final TreeNode<TSGNode> toChild = TreeNode.create(
toChildData, fromChild.nProperties());
currentTo.addChildNode(toChild, i);
toStack.push(toChild);
fromStack.push(fromChild);
}
}
}
}
示例5: getRandomElement
import org.apache.commons.lang.math.RandomUtils; //導入方法依賴的package包/類
@Override
public T getRandomElement() {
final double randomPick = RandomUtils.nextDouble();
double sum = 0;
for (final Entry<T, Double> element : cache.entrySet()) {
sum += element.getValue();
if (sum >= randomPick) {
return element.getKey();
}
}
checkArgument(false,
"Should not reach this point. When adding probabilities, they should sum to 1.");
return null;
}
示例6: sampleScopes
import org.apache.commons.lang.math.RandomUtils; //導入方法依賴的package包/類
/**
* @param scopes
* @return
*/
private List<Entry<Scope, String>> sampleScopes(
final Multimap<Scope, String> scopes) {
final List<Entry<Scope, String>> selectedScopes = Lists
.newArrayList();
// Sample
for (final Entry<Scope, String> variable : scopes.entries()) {
if (RandomUtils.nextDouble() > SAMPLING_RATIO) {
continue;
}
selectedScopes.add(variable);
}
return selectedScopes;
}
示例7: appendToDebugFile
import org.apache.commons.lang.math.RandomUtils; //導入方法依賴的package包/類
private synchronized static void appendToDebugFile(final StringBuffer buf) {
try {
debugFile.write(buf.toString().getBytes());
if (RandomUtils.nextDouble() < 0.01) {
debugFile.flush();
}
} catch (final IOException e) {
LOGGER.warning(ExceptionUtils.getFullStackTrace(e));
}
}
示例8: appendToStatsFile
import org.apache.commons.lang.math.RandomUtils; //導入方法依賴的package包/類
private synchronized static void appendToStatsFile(final StringBuffer buf) {
try {
fileStatsOutput.write(buf.toString().getBytes());
if (RandomUtils.nextDouble() < 0.01) {
fileStatsOutput.flush();
}
} catch (final IOException e) {
LOGGER.warning(ExceptionUtils.getFullStackTrace(e));
}
}
示例9: getRandomIndex
import org.apache.commons.lang.math.RandomUtils; //導入方法依賴的package包/類
/**
* Get a random index when selecting from the given unnormalized
* log2-probabilities.
*
* @param log2ProbWeights
* @return
*/
public static int getRandomIndex(final double[] log2ProbWeights) {
double max = Double.NEGATIVE_INFINITY;
for (final double weight : log2ProbWeights) {
if (max < weight) {
max = weight;
}
}
final double[] weights = new double[log2ProbWeights.length];
double sum = 0;
for (int i = 0; i < log2ProbWeights.length; i++) {
final double prob = Math.pow(2, log2ProbWeights[i] - max);
sum += prob;
weights[i] = prob;
}
final double randomPoint = RandomUtils.nextDouble() * sum;
double partialSum = 0;
for (int i = 0; i < log2ProbWeights.length; i++) {
partialSum += weights[i];
if (partialSum >= randomPoint) {
return i;
}
}
throw new IllegalStateException("Should not have reached here.");
}
示例10: setUp
import org.apache.commons.lang.math.RandomUtils; //導入方法依賴的package包/類
@Override
protected void setUp() throws Exception {
strings = new String[5];
doubles = new double[5];
for (int i = 0; i < 5; i++) {
strings[i] = RandomStringUtils.random(8);
doubles[i] = RandomUtils.nextDouble();
}
}
示例11: random
import org.apache.commons.lang.math.RandomUtils; //導入方法依賴的package包/類
@Override
public Double random() {
return RandomUtils.nextDouble();
}
示例12: randomDouble
import org.apache.commons.lang.math.RandomUtils; //導入方法依賴的package包/類
public static double randomDouble(int min, int max) {
return RandomUtils.nextDouble() + RandomUtils.nextInt(max - min);
}
示例13: sampleAt
import org.apache.commons.lang.math.RandomUtils; //導入方法依賴的package包/類
/**
* Sample the given node and change status if needed.
*
* @param node
* the node where to sample (if it is a root or not)
* @param root
* the current root of the tree.
* @return the probability of joining at this point.
*/
public double sampleAt(final TreeNode<TSGNode> node,
final TreeNode<TSGNode> root) {
checkNotNull(node);
checkNotNull(root);
checkArgument(node != root,
"The given node should not be the root but its parent root");
final boolean wasRootBefore = node.getData().isRoot;
node.getData().isRoot = false;
final TreeNode<TSGNode> joinedTree = TSGNode.getSubTreeFromRoot(root);
node.getData().isRoot = true;
final TreeNode<TSGNode> splitTree1 = TSGNode.getSubTreeFromRoot(root);
final TreeNode<TSGNode> splitTree2 = TSGNode.getSubTreeFromRoot(node);
final double log2ProbJoined = sampleGrammar
.computeRulePosteriorLog2Probability(joinedTree, !wasRootBefore);
final double log2ProbSplit = sampleGrammar
.computeRulePosteriorLog2Probability(splitTree1, wasRootBefore)
+ sampleGrammar.computeRulePosteriorLog2Probability(splitTree2,
wasRootBefore);
final double joinTheshold;
if (!Double.isInfinite(log2ProbJoined)) {
final double splitLog2Prob = log2ProbJoined
- StatsUtil.log2SumOfExponentials(log2ProbJoined,
log2ProbSplit);
joinTheshold = Math.pow(2, splitLog2Prob);
node.getData().isRoot = RandomUtils.nextDouble() > joinTheshold;
} else {
// Split if probJoined == 0, regardless of the splitting prob.
joinTheshold = 0;
node.getData().isRoot = true;
}
// Add/remove trees from grammar
if (wasRootBefore != node.getData().isRoot) {
if (wasRootBefore) {
checkArgument(sampleGrammar.removeTree(splitTree1));
checkArgument(sampleGrammar.removeTree(splitTree2));
sampleGrammar.addTree(joinedTree);
} else {
checkArgument(sampleGrammar.removeTree(joinedTree));
sampleGrammar.addTree(splitTree1);
sampleGrammar.addTree(splitTree2);
}
}
return joinTheshold;
}
示例14: main
import org.apache.commons.lang.math.RandomUtils; //導入方法依賴的package包/類
/**
* @param args
*/
public static void main(String[] args) {
if (args.length != 3) {
System.err
.println("Usage <projectDir> variable|method|type|all <nExperiments>");
return;
}
final SelectionSuggestionEval sse = new SelectionSuggestionEval(
new File(args[0]), new JavaTokenizer(),
ScopesTUI.getScopeExtractorByName(args[1]));
final int nExperiments = Integer.parseInt(args[2]);
final ParallelThreadPool pt = new ParallelThreadPool();
for (int i = 0; i < nExperiments; i++) {
pt.pushTask(new Runnable() {
@Override
public void run() {
try {
sse.runSingleExperiment();
} catch (IOException e) {
LOGGER.warning("Failed to run single experiment "
+ ExceptionUtils.getFullStackTrace(e));
}
}
});
if (RandomUtils.nextDouble() < .1) {
pt.pushTask(new Runnable() {
@Override
public void run() {
sse.results.printResults();
}
});
}
}
pt.waitForTermination();
sse.results.printResults();
}
示例15: handleEntityDyingWithGive
import org.apache.commons.lang.math.RandomUtils; //導入方法依賴的package包/類
private void handleEntityDyingWithGive(EntityDeathEvent event) {
List<ItemStack> newDrops = new ArrayList<>();
ItemStack[] array = new ItemStack[6];
System.arraycopy(event.getEntity().getEquipment().getArmorContents(), 0, array, 0, 4);
array[4] = event.getEntity().getEquipment().getItemInMainHand();
array[5] = event.getEntity().getEquipment().getItemInOffHand();
event.getEntity().getEquipment().setBootsDropChance(0.0F);
event.getEntity().getEquipment().setLeggingsDropChance(0.0F);
event.getEntity().getEquipment().setChestplateDropChance(0.0F);
event.getEntity().getEquipment().setHelmetDropChance(0.0F);
event.getEntity().getEquipment().setItemInMainHandDropChance(0.0F);
event.getEntity().getEquipment().setItemInOffHandDropChance(0.0F);
for (ItemStack is : array) {
if (is == null || is.getType() == Material.AIR || !is.hasItemMeta()) {
continue;
}
CustomItem ci = CustomItemUtil.getCustomItemFromItemStack(is);
if (ci != null) {
newDrops.add(ci.toItemStack());
if (ci.isBroadcastOnFind() && event.getEntity().getKiller() != null) {
broadcastMessage(event.getEntity().getKiller(), ci.toItemStack());
}
continue;
}
SocketGem socketGem = SocketGemUtil.getSocketGemFromItemStack(is);
if (socketGem != null) {
newDrops.add(new SocketItem(is.getType(), socketGem));
continue;
}
IdentityTome identityTome = new IdentityTome();
if (is.isSimilar(identityTome)) {
newDrops.add(identityTome);
continue;
}
UnidentifiedItem unidentifiedItem = new UnidentifiedItem(is.getType());
if (is.isSimilar(unidentifiedItem)) {
newDrops.add(unidentifiedItem);
continue;
}
Tier t = TierUtil.getTierFromItemStack(is);
if (t != null && RandomUtils.nextDouble() < t.getDropChance()) {
ItemStack nis = is.getData().toItemStack(1);
nis.setItemMeta(is.getItemMeta());
nis.setDurability(ItemStackUtil.getDurabilityForMaterial(is.getType(),
t.getMinimumDurabilityPercentage(),
t.getMaximumDurabilityPercentage()));
if (t.isBroadcastOnFind()) {
if (event.getEntity().getKiller() != null) {
broadcastMessage(event.getEntity().getKiller(), nis);
}
}
newDrops.add(nis);
}
}
for (ItemStack itemStack : newDrops) {
if (itemStack.getType() == Material.AIR) {
continue;
}
World w = event.getEntity().getWorld();
Location l = event.getEntity().getLocation();
w.dropItemNaturally(l, itemStack);
}
}