0

一些随机数生成,想知道我是否可以使用循环来生成所有这些数字,而不是写出所有整数。还有比这更好的方法吗?我可以把它保持原样吗?

    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);
4

6 回答 6

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);
}
于 2013-04-04T07:03:52.620 回答
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] + " , ");
        }

    }
}
于 2013-04-04T07:08:13.623 回答
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);
    }
}

但是,这将取决于您的具体需求。

于 2013-04-04T07:09:30.353 回答
0

我们可以简单地使用 java 的 random() 并这样做:

公共静态 int[] x = new int[32];

公共静态 int[] y = new int[32];

for(int i = 0; i < 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

于 2013-04-04T13:46:53.827 回答
0

您可以按照此处所述使用 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);
}
于 2013-04-04T08:42:01.110 回答
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 而不是 1000,您可以使用另一个Math.random()乘以 1000。randomNumbersList.add((int)Math.random()*math.random()*1000);解决方案也是如此。

请看一下这个Ideone 片段

于 2013-04-04T07:05:34.270 回答