我正在寻找一种使用java的算法,类似于Paul的预测。看起来,如果我们给算法两个数字,它将从这两个数字中随机给出一个数字。
发布于 2011-03-26 12:09:51
这很好……
public static void main(String[] args) {
double left = Math.random();
System.out.println("left : "+left);
double right = Math.random();
System.out.println("right : "+right);
System.out.println(pickOne(left, right));
}
public static double pickOne(double left, double right) {
return Math.random() < Math.random() ? left : right;
}发布于 2011-03-26 10:25:58
很难从你的问题中确切地说出你想要什么,但看起来你要么想要这样的东西:
public static int pickOne(int left, int right) {
return Math.random() < 0.5 ? left : right;
}或者:
public static int pickRandomFromRange(int lowerBound, int upperBound) {
return (int)(Math.random() * upperBound) + lowerBound;
}https://stackoverflow.com/questions/5439958
复制相似问题