我创建了这个程序来打印介于-100和100之间的数字,但我想知道这是否是正确的方法,或者必须在限制(预先指定)a和b之间完成?
public class RandomNumber {
public static void main (String [] args){
for (int i = 1; i <= 10 ; i++)
{
int Random = (int)(Math.random()*-100);
System.out.println(Random);
}
for (int i = 1; i <= 10 ; i++)
{
int Random1 = (int)(Math.random()*100);
System.out.println(Random1);
}
}
}
答案 0 :(得分:3)
如果要生成介于-100和100之间的数字:
public class RandomNumber {
public static void main (String [] args){
for (int i = 1; i <= 10 ; i++)
{
int Random = (int)(Math.random()*(200 + 1)) - 100;
System.out.println(Random);
}
}
}
这是有效的,因为Math.random()
会在0
和1
之间生成一个值。然后将该值乘以200并从中减去100。
示例:
((0.75) * (200 + 1)) - 100 = 150 - 100 = 50
如果您想要一个介于a
(较小)和b
(较大)之间的数字,请尝试:
int Random = (int)(Math.random() * (b - a + 1)) + a;
示例(a = 30, b = 70
):
Random = 0
(0)(70 - 30 + 1) + 30 = 0 + 30 = 30
Random = 0.5
(0.5)(70 - 30 + 1) + 30 = 20 + 30 = 50
Random = 1
(0.999999)(70 - 30 + 1) + 30 = 40 + 30 = 70
答案 1 :(得分:1)
最好的方法是
Random r = new Random();
int n = -100 + (int)(r.nextFloat() * 200);
因为你介绍的范围是200个单位。 nextFloat
将返回介于0.0和1.0之间的值,并将其乘以200并减去100和BAM! -100到100!
答案 2 :(得分:1)
我认为这是最简单的:
public static final int getRandomBetweenInclusive(int min, int max) {
return (min + (int)(Math.random() * ((max - min) + 1)));
}
用
调用它int random = RandomNumberUtil.getRandomBetweenInclusive(-100, 100);
它实际上来自this answer。它非常聪明和简洁。
我写了这个测试应用程序,以确认它可以均匀地分配所有可能性:
import java.util.Iterator;
import java.util.TreeMap;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
/**
<P>{@code java RandomNumberTest}</P>
**/
public class RandomNumberTest {
private static final int tryCount = 1_000_000;
public static final void main(String[] ignored) {
Map<Integer,Integer> randCountMap = new TreeMap<Integer,Integer>();
for(int i = 0; i < tryCount; i++) {
int rand = getRandomBetweenInclusive(-10, 10);
int value = ((!randCountMap.containsKey(rand)) ? 1
: randCountMap.get(rand) + 1);
randCountMap.put(rand, value);
}
Iterator<Integer> allIntItr = randCountMap.keySet().iterator();
List<NumWithCount> numWcountList = new ArrayList<NumWithCount>(randCountMap.size());
while(allIntItr.hasNext()) {
Integer I = allIntItr.next();
int count = randCountMap.get(I);
NumWithCount nwc = new NumWithCount(I, count);
numWcountList.add(nwc);
}
Iterator<NumWithCount> intWCountItr = numWcountList.iterator();
while(intWCountItr.hasNext()) {
NumWithCount numWCount = intWCountItr.next();
float pct = (float)numWCount.occurances / tryCount * 100;
System.out.println(numWCount.num + ": " + numWCount.occurances + " " + String.format("%.3f", pct) + "%");
}
}
public static final int getRandomBetweenInclusive(int min, int max) {
return (min + (int)(Math.random() * ((max - min) + 1)));
}
}
class NumWithCount {
public final int num;
public final int occurances;
public NumWithCount(int num, int occurances) {
this.num = num;
this.occurances = occurances;
}
public String toString() {
return "num=" + num + ", occurances=" + occurances;
}
}
输出:
[R:\jeffy\programming\sandbox\xbnjava]java RandomNumberTest 1000000
-10: 47622 4.762%
-9: 48024 4.802%
-8: 47579 4.758%
-7: 47598 4.760%
-6: 47660 4.766%
-5: 47299 4.730%
-4: 47635 4.764%
-3: 47675 4.767%
-2: 47678 4.768%
-1: 47757 4.776%
0: 47557 4.756%
1: 47888 4.789%
2: 47644 4.764%
3: 47177 4.718%
4: 47381 4.738%
5: 47836 4.784%
6: 47539 4.754%
7: 47561 4.756%
8: 47520 4.752%
9: 47481 4.748%
10: 47889 4.789%
答案 3 :(得分:1)
拥有一个可以重复使用的更通用的功能总是很好:
private static int getRandomNumber(int a, int b) {
if (b < a)
return getRandomNumber(b, a);
return a + (int) ((1 + b - a) * Math.random());
}
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
System.out.format("%d ", getRandomNumber(-100, 100));
}
}
答案 4 :(得分:0)
这实际上取决于你想要达到的目标。如果你想在同一个循环中打印介于-100和+100之间的数字,你可以这样做:
for (int i = 1; i <= 10 ; i++)
{
int random = (int)(Math.random()*200-100);
System.out.println(random);
}