一些随机数生成,想知道我是否可以使用循环来生成所有这些数字,而不是将所有整数写出来。还有比这更好的方法吗?我可以直接离开吗?
public static int rx1 = 0+(int)(Math.random()*760);
public static int ry1 = 0+(int)(Math.random()*555);
public static int rx2 = 0+(int)(Math.random()*760);
public static int ry2 = 0+(int)(Math.random()*555);
public static int rx3 = 0+(int)(Math.random()*760);
public static int ry3 = 0+(int)(Math.random()*555);
public static int rx4 = 0+(int)(Math.random()*760);
public static int ry4 = 0+(int)(Math.random()*555);
public static int rx5 = 0+(int)(Math.random()*760);
public static int ry5 = 0+(int)(Math.random()*555);
public static int rx6 = 0+(int)(Math.random()*760);
public static int ry6 = 0+(int)(Math.random()*555);
public static int rx7 = 0+(int)(Math.random()*760);
public static int ry7 = 0+(int)(Math.random()*555);
public static int rx8 = 0+(int)(Math.random()*760);
public static int ry8 = 0+(int)(Math.random()*555);
public static int rx9 = 0+(int)(Math.random()*760);
public static int ry9 = 0+(int)(Math.random()*555);
public static int rx10 = 0+(int)(Math.random()*760);
public static int ry10 = 0+(int)(Math.random()*555);
public static int rx11 = 0+(int)(Math.random()*760);
public static int ry11 = 0+(int)(Math.random()*555);
public static int rx12 = 0+(int)(Math.random()*760);
public static int ry12 = 0+(int)(Math.random()*555);
public static int ry13 = 0+(int)(Math.random()*555);
public static int rx13 = 0+(int)(Math.random()*760);
public static int rx14 = 0+(int)(Math.random()*555);
public static int ry14 = 0+(int)(Math.random()*555);
public static int rx15 = 0+(int)(Math.random()*760);
public static int ry15 = 0+(int)(Math.random()*555);
public static int rx16 = 0+(int)(Math.random()*760);
public static int ry16 = 0+(int)(Math.random()*555);
public static int rx17 = 0+(int)(Math.random()*760);
public static int ry17 = 0+(int)(Math.random()*555);
public static int rx18 = 0+(int)(Math.random()*760);
public static int ry18 = 0+(int)(Math.random()*555);
public static int rx19 = 0+(int)(Math.random()*760);
public static int ry19 = 0+(int)(Math.random()*555);
public static int rx20 = 0+(int)(Math.random()*760);
public static int ry21 = 0+(int)(Math.random()*555);
public static int rx21 = 0+(int)(Math.random()*760);
public static int ry22 = 0+(int)(Math.random()*555);
public static int rx22 = 0+(int)(Math.random()*760);
public static int ry23 = 0+(int)(Math.random()*555);
public static int rx23 = 0+(int)(Math.random()*760);
public static int ry24 = 0+(int)(Math.random()*555);
public static int rx24 = 0+(int)(Math.random()*760);
public static int ry25 = 0+(int)(Math.random()*555);
public static int rx25 = 0+(int)(Math.random()*760);
public static int ry26 = 0+(int)(Math.random()*555);
public static int rx27 = 0+(int)(Math.random()*760);
public static int ry28 = 0+(int)(Math.random()*555);
public static int rx28 = 0+(int)(Math.random()*760);
public static int ry29 = 0+(int)(Math.random()*555);
public static int ry30 = 0+(int)(Math.random()*555);
public static int rx30 = 0+(int)(Math.random()*760);
public static int rx31 = 0+(int)(Math.random()*555);
public static int ry31 = 0+(int)(Math.random()*555);
public static int rx32 = 0+(int)(Math.random()*760);
public static int ry32 = 0+(int)(Math.random()*555);
答案 0 :(得分:4)
将它们放在两个数组中:
public static int[] x = new int[32];
public static int[] y = new int[32];
for(int i = 0; i < 32; i++)
{
x[i] = (int)(Math.random()*760);
y[i] = (int)(Math.random()*555);
}
答案 1 :(得分:1)
您可以使用相同的数组。这是示例程序
package com.stackoverflow.test;
public class RandomCheck {
public static void main(String args[]) {
int[] tempArray = new int[64];
for (int i = 0; i < 64; i++) {
if (i % 2 == 0)
tempArray[i] = (int) (Math.random() * 760);
else
tempArray[i] = (int) (Math.random() * 555);
}
for (int i = 0; i < 64; i++) {
System.out.print(tempArray[i] + " , ");
}
}
}
答案 2 :(得分:0)
您可以将for(;;)
循环与ArrayList
一起使用。从问题中不清楚是否需要这些数字的固定数量。
import java.util.ArrayList;
import java.lang.Math;
public class Main{
public static void main(String args[]){
int n = 32; // No of random numbers requried for x and y
ArrayList<Integer> randomNumbersListX = new ArrayList<Integer>();
ArrayList<Integer> randomNumbersListY = new ArrayList<Integer>();
for(int i=1; i<=n ; i++){
randomNumbersListX.add((int)(Math.random()*760));
randomNumbersListY.add((int)(Math.random()*555));
}
for(int i=0; i<randomNumbersListX.size() ; i++){
System.out.println("rx"+(i+1)+" "+randomNumbersListX.get(i));
System.out.println("ry"+(i+1)+" "+randomNumbersListY.get(i));
}
}
}
为了使变量乘以而不是1000,您可以使用另一个Math.random()
乘以1000.因此randomNumbersList.add((int)Math.random()*math.random()*1000);
将成为解决方案。
请查看此Ideone snippet
答案 3 :(得分:0)
这将是一种方法:
public static final int RX_SIZE = 32;
public static final int RY_SIZE = 32;
public static int rx[] = new int[RX_SIZE];
public static int ry[] = new int[RY_SIZE];
static {
for(int i = 0; i < RX_SIZE; i++) {
rx[i] = 0+(int)(Math.random()*760);
}
for(int i = 0; i < RY_SIZE; i++) {
ry[i] = 0+(int)(Math.random()*555);
}
}
但这取决于您的具体需求。
答案 4 :(得分:0)
您可以按照here
所述使用Commons Math示例,以下将生成一个50个长整数的随机序列,介于1和1,000,000之间,使用当前时间(以毫秒为单位)作为JDK PRNG的种子:
RandomData randomData = new RandomDataImpl();
for (int i = 0; i < 1000; i++) {
value = randomData.nextLong(1, 1000000);
}
答案 5 :(得分:0)
我们可以简单地使用java的random()并将其作为:
public static int [] x = new int [32];
public static int [] y = new int [32];
for(int i = 0; i&lt; 32; i ++)
{
x [i] =(int)(Math.random()* 760);
y[i] = (int)(Math.random()*555);
}
有关Math.random()的更多信息,可以点击链接http://www.w3schools.com/jsref/jsref_random.asp http://msdn.microsoft.com/en-us/library/ie/41336409(v=vs.94).aspx