英文:
How can I write an array generator that generates numbers in certain column values?
问题
public class scratch {
public static void main(String[] args) {
double values[][] = new double[10][19];
for (int i = 0; i < values.length; i++) {
for (int j = 0; j < values[i].length; j++) {
values[i][j] = ((double) (Math.random()));
System.out.print(values[i][j] + " ; ");
}
System.out.println();
}
}
}
英文:
Hi there I wrote this array generator and would like to rewrite it so that you can set the value ranges from which the individual column values come.
So as an example: the first columns are either 0 or 1, the values to the right of them are either 3 or 7 and the values of the other columns are between 0 and 1 (so as it is at the moment for all numbers).
1 ; 3 ; 0.68618 ; 0.98135 ; 0.25489 ; ...
1 ; 7 ; 0.32481 ; 0.25871 ; 0.14697 ; ...
0 ; 7 ; 0.96125 ; 0.36815 ; 0.24863 ; ...
......
public class scratch{
public static void main(String[] args) {
double values[][] = new double[10][19];
for (int i = 0; i < values.length; i++) {
for (int j = 0; j < values[i].length; j++) {
values[i][j] = ((double) (Math.random()));
System.out.print(values[i][j]+" ; ");
}
System.out.println();
}
}
}
PS: please excuse my bad english
答案1
得分: 1
只需在迭代时检查列的索引,第一列为 j == 0
,第二列为 j == 1
。要在0和1之间或在3和7之间交替,只需创建一个随机数,如果小于0.5,则取0,否则取1(或3或7)。
public static void main(String[] args) {
double values[][] = new double[10][19];
for (int i = 0; i < values.length; i++) {
for (int j = 0; j < values[i].length; j++) {
if(j==0){
values[i][j] = Math.random() < 0.5 ? 0 : 1;
}
else if(j==1){
values[i][j] = Math.random() < 0.5 ? 3 : 7;
}
else{
values[i][j] = Math.random(); // 要将其四舍五入到4位小数:Math.floor(Math.random() * 10000) / 10000;
}
System.out.print(values[i][j]+ " ; ");
}
System.out.println();
}
}
编辑
您可以扩展这种方法,从四个数字中选择一个(在 [23, 42, 69, 1001] 之间),使用类似以下的方式:
if(j==0){
double x = Math.random();
values[i][j] = x < 0.25 ? 23 : x < 0.5 ? 42 : x < 0.75 ? 69 : 1001;
}
但是,随着选择的数字变得更多,这将很快变得难以阅读。因此,我建议您将可能的值存储在数组中,并随机选择一个索引。这样,如果需要,您可以添加更多的值,而不需要每次更改代码:
if(j==0){
int[] myValues = {23, 42, 69, 1001};
values[i][j] = myValues[ (int) (Math.random()*myValues.length)];
}
英文:
Just check index of column while iterating, first column is when j == 0
and second column is when j == 1
. And to alternate between 0 and 1 or between 3 and 7 just create a random number and if it is less than 0.5 take 0 and if not take 1 (or 3 or 7 respectivley)
public static void main(String[] args) {
double values[][] = new double[10][19];
for (int i = 0; i < values.length; i++) {
for (int j = 0; j < values[i].length; j++) {
if(j==0){
values[i][j] = Math.random() < 0.5 ? 0 : 1;
}
else if(j==1){
values[i][j] = Math.random() < 0.5 ? 3 : 7;
}
else{
values[i][j] = Math.random(); // to round it to 4 decimal places: Math.floor(Math.random() * 10000) / 10000;
}
System.out.print(values[i][j]+" ; ");
}
System.out.println();
}
}
EDIT
You can extend this aproach
if(j==0){
values[i][j] = Math.random() < 0.5 ? 0 : 1;
}
to choose one number out of four (below between [23, 42, 69, 1001] ) by using something like:
if(j==0){
double x = Math.random();
values[i][j] = x < 0.25 ? 23 : x < 0.5 ? 42 : x < 0.75 ? 69 : 1001;
}
But this will quickly become illegible, especially if you want to choose from even more numbers later on. Therfor I would suggest you to store your possible values in an array and randomly select an index. This way you can add more values if necessary and doesen't need to change the code each time:
if(j==0){
int[] myValues = {23, 42, 69, 1001};
values[i][j] = myValues[ (int) (Math.random()*myValues.length)];
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论