Java 生成随机数的更好方法
一些随机数的生成,我想知道我是否可以使用一个循环来生成所有这些数字,而不是把所有的整数都写出来。还有比这更好的方法吗?我能不能让它保持原样Java 生成随机数的更好方法,java,loops,random,integer,Java,Loops,Random,Integer,一些随机数的生成,我想知道我是否可以使用一个循环来生成所有这些数字,而不是把所有的整数都写出来。还有比这更好的方法吗?我能不能让它保持原样 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 ry
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);
只需将它们放在两个阵列中:
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);
}
publicstaticint[]x=newint[32];
公共静态int[]y=新int[32];
对于(int i=0;i<32;i++)
{
x[i]=(int)(Math.random()*760);
y[i]=(int)(Math.random()*555);
}
只需将它们放在两个数组中:
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);
}
publicstaticint[]x=newint[32];
公共静态int[]y=新int[32];
对于(int i=0;i<32;i++)
{
x[i]=(int)(Math.random()*760);
y[i]=(int)(Math.random()*555);
}
您可以将与数组列表一起用于(;;)
循环。问题不清楚你是否想要固定数量的数字
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));
}
}
}
import java.util.ArrayList;
导入java.lang.Math;
公共班机{
公共静态void main(字符串参数[]){
int n=32;//x和y所需的随机数的数目
ArrayList RandomNumberListX=新的ArrayList();
ArrayList RandomNumberListy=新的ArrayList();
对于(int i=1;i您可以使用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));
}
}
}
import java.util.ArrayList;
导入java.lang.Math;
公共班机{
公共静态void main(字符串参数[]){
int n=32;//x和y所需的随机数的数目
ArrayList RandomNumberListX=新的ArrayList();
ArrayList RandomNumberListy=新的ArrayList();
对于(inti=1;i,您可以使用一个数组来进行相同的操作
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] + " , ");
}
}
}
package com.stackoverflow.test;
公共类随机检查{
公共静态void main(字符串参数[]){
int[]tempArray=newint[64];
对于(int i=0;i<64;i++){
如果(i%2==0)
tempArray[i]=(int)(Math.random()*760);
其他的
tempArray[i]=(int)(Math.random()*555);
}
对于(int i=0;i<64;i++){
System.out.print(tempArray[i]+“,”);
}
}
}
您可以使用数组进行相同的操作。下面是示例程序
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] + " , ");
}
}
}
package com.stackoverflow.test;
公共类随机检查{
公共静态void main(字符串参数[]){
int[]tempArray=newint[64];
对于(int i=0;i<64;i++){
如果(i%2==0)
tempArray[i]=(int)(Math.random()*760);
其他的
tempArray[i]=(int)(Math.random()*555);
}
对于(int i=0;i<64;i++){
System.out.print(tempArray[i]+“,”);
}
}
}
这将是一种方法:
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);
}
}
public static final int RX_SIZE=32;
公共静态最终整数大小=32;
公共静态整数rx[]=新整数[rx_SIZE];
公共静态整数ry[]=新整数[ry_SIZE];
静止的{
对于(int i=0;i
但是,这将取决于您的具体需求。这将是一种方法:
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);
}
}
public static final int RX_SIZE=32;
公共静态最终整数大小=32;
公共静态整数rx[]=新整数[rx_SIZE];
公共静态整数ry[]=新整数[ry_SIZE];
静止的{
对于(int i=0;i
不过,这将取决于您的具体需求。您可以使用Commons Math来实现这一点,如前所述
例如,以下内容将使用当前时间(以毫秒为单位)作为JDK PRNG的种子,生成1到1000000之间50个长整数的随机序列:
RandomData randomData = new RandomDataImpl();
for (int i = 0; i < 1000; i++) {
value = randomData.nextLong(1, 1000000);
}
RandomData RandomData=new RandomDataImpl();
对于(int i=0;i<1000;i++){
value=randomData.nextLong(1000000);
}
如前所述,您可以使用Commons Math进行此操作
例如,以下内容将使用当前时间(以毫秒为单位)作为JDK PRNG的种子,生成1到1000000之间50个长整数的随机序列:
RandomData randomData = new RandomDataImpl();
for (int i = 0; i < 1000; i++) {
value = randomData.nextLong(1, 1000000);
}
RandomData RandomData=new RandomDataImpl();
对于(int i=0;i<1000;i++){
value=randomData.nextLong(1000000);
}
我们可以简单地使用java的random()并按如下方式执行:
公共静态int[]x=新int[32]
公共静态int[]y=新int[32]
对于(int i=0;i<32;i++)
{
x[i]=(int)(Math.random()*760)
}
有关Math.random()的更多信息,请访问以下链接
我们可以简单地使用java的random()并按如下方式操作:
公共静态int[]x=新int[32]
公共静态int[]y=新int[32]
对于(int i=0;i<32;i++)
{
x[i]=(int)(Math.random()*760)
}
有关Math.random()的更多信息,请访问以下链接
当然可以保持原样。代码没有任何用处。如果您描述了您想要实现的目标,我们可能会有更好的建议。您想做什么?标记为“slick”--为什么?不,这是唯一的方法。为什么你需要这么多随机数来保存这么多随机变量?你是在向这里的随机人问一些随机问题吗?当然可以保持原样。代码没有任何用处。如果你描述你想要实现的目标,我们可能会有更好的建议。你在尝试什么o做什么?标记为“光滑”--为什么?不,这是唯一的一种方法。为什么你需要这么多随机数来保存这么多随机变量?你是在向这里的一些随机人问一些随机问题吗?谢谢你的回答,这帮助了很多。尝试学习如何制作代码块。我按照标准方法编辑了你的答案。谢谢你的回答这很有帮助。试着学习如何制作代码块。