[英]Make an array of random integers based on seed in Java
我需要创建一个整数数组(双精度和浮点数也很好,但我认为没有区别),因为我需要对它们进行某些数学运算,例如*和+。 我试图用随机种子填充数组(即1337 5443,我需要使用它们),但是我无法将随机变量转换为int,也无法添加或乘以随机变量。 因此,从本质上讲,我需要根据特定种子制作一个随机数数组,并且还需要能够对列表中的每个元素进行数学运算。 这是到目前为止我所做的一个示例:
import java.util.Random;
public class{
public static int a [] = new int [101];
public static void main(String[] Args){
for(int i = 0; i <= 100; i++){
Random ran1 = new Random(1337);
a [i] = ran1;//THIS IS THE PROBLEM (incompatible types)
}
int sum = a[5] + a[8] * a[10];//this won't acctually be included, it's just an example
}
}
你不分配Random
到int
-你需要调用nextInt
,传递int
,让之间的范围0
和约束减1
。
a[i] = ran1.nextInt(10); // 0-9 or substitute what you want for 10
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.