英文:
2D Array with random values, but the values in two of the columns should not be the same
问题
public class HelloWorld {
public static void main(String[] args) {
Number[][] values = new Number[10][26];
for (int i = 0; i < values.length; i++) {
for (int j = 0; j < values[i].length; j++) {
double x = Math.random();
double y;
if (x < 0.25) {
y = 1;
} else if (x < 0.5) {
y = 0.74;
} else if (x < 0.75) {
y = 0.49;
} else {
y = 0.01;
}
if (j == 0) {
values[i][j] = Math.random() < 0.5 ? 0 : 1;
} else if (j == 1) {
values[i][j] = (int) Math.floor(((Math.random() * (250000000 - 500000)) + 500000));
} else if (j == 2) {
values[i][j] = (int) (Math.random() * (23)) + 1;
} else if (j == 3) {
values[i][j] = x < 0.25 ? 1 : x < 0.5 ? 3 : x < 0.75 ? 7 : 20;
} else if (j == 4) {
do {
y = Math.random() < 0.25 ? 1 : Math.random() < 0.5 ? 3 : Math.random() < 0.75 ? 7 : 20;
} while (x == 1 && y == 1);
values[i][j] = y;
} else {
values[i][j] = Math.floor(Math.random() * 100000) / 100000;
}
System.out.print(values[i][j] + " ; ");
}
System.out.println();
}
}
}
英文:
I have this number generator, which generates me a 2d matrix with random values. The values in column 4 and 5 are either 1, 3, 7 or 20. But when column 4 is a 1, column 5 should be either 3, 7 or 20 (they should never have the same values).
I tried it with the double x and double y declaration under the second for-loop, but that doesn't work. So in my next try, I would try it with a do-while-loop, but I don't know, how to implement that loop in my code.
public class HelloWorld{
public static void main(String[] args) {
Number[][] values = new Number[10][26];
for (int i = 0; i < values.length; i++) {
for (int j = 0; j < values[i].length; j++) {
double x = Math.random();
double y = x < 0.25 ? 1 : x < 0.5 ? 0.74 : x < 0.75 ? 0.49 : 0.01;
if (j == 0) {
values[i][j] = Math.random() < 0.5 ? 0 : 1;
} else if (j == 1) {
values[i][j] = (int)Math.floor(((Math.random() * (250000000 - 500000)) + 500000));
} else if (j == 2) {
values[i][j] = (int)(Math.random() * (23)) + 1;
} else if (j == 3) {
values[i][j] = x < 0.25 ? 1 : x < 0.5 ? 3 : x < 0.75 ? 7 : 20;
} else if (j == 4) {
values[i][j] = y < 0.25 ? 1 : y < 0.5 ? 3 : y < 0.75 ? 7 : 20;
} else {
values[i][j] = Math.floor(Math.random() * 100000) / 100000;
}
System.out.print(values[i][j] + " ; ");
}
System.out.println();
}
}
}
答案1
得分: 1
以下是翻译好的部分:
- 在内循环之外,声明一个给定行的所有可能整数的列表。
- 对于第3列或第4列,从0到列表大小的范围内获取一个随机索引。
- 从列表中取出该索引处的元素,并赋值给您的矩阵。
- 从列表中删除该索引处的元素,以防止后面重复使用。
请注意,这里我使用了 Random.nextInt(sizeOfList)
,它比 Math.random()
更清晰。
public class MyClass {
public static void main(String[] args) {
Number[][] values = new Number[10][26];
Random r = new Random();
for (int i = 0; i < values.length; i++) {
List<Integer> possibleInts = new ArrayList<Integer>(Arrays.asList(1, 3, 7, 20));
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] = (int)Math.floor(((Math.random() * (250000000 - 500000)) + 500000));
} else if (j == 2) {
values[i][j] = (int)(Math.random() * (23)) + 1;
} else if (j == 3) {
int index = r.nextInt(possibleInts.size());
values[i][j] = possibleInts.get(index);
possibleInts.remove(index);
} else if (j == 4) {
int index = r.nextInt(possibleInts.size());
values[i][j] = possibleInts.get(index);
possibleInts.remove(index);
} else {
values[i][j] = Math.floor(Math.random() * 100000) / 100000;
}
System.out.print(values[i][j] + " ; ");
}
System.out.println();
}
}
}
英文:
There are probably many different ways of achieving the same result. The solution I came up with looks like so:
- Declare a list of all possible ints for a given row - outside of the inner loop
- For columns 3 or 4 get a random index from range 0 - sizeOfList
- Take element with that index from the List and assign to your matrix
- Remove element with that index from the List so it isn't reused later on
Note that I used Random.nextInt(sizeOfList)
here, it's just cleaner than Math.random()
.
public class MyClass {
public static void main(String[] args) {
Number[][] values = new Number[10][26];
Random r = new Random();
for (int i = 0; i < values.length; i++) {
List<Integer> possibleInts = new ArrayList<Integer>(Arrays.asList(1, 3, 7, 20));
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] = (int)Math.floor(((Math.random() * (250000000 - 500000)) + 500000));
} else if (j == 2) {
values[i][j] = (int)(Math.random() * (23)) + 1;
} else if (j == 3) {
int index = r.nextInt(possibleInts.size());
values[i][j] = possibleInts.get(index);
possibleInts.remove(index);
} else if (j == 4) {
int index = r.nextInt(possibleInts.size());
values[i][j] = possibleInts.get(index);
possibleInts.remove(index);
} else {
values[i][j] = Math.floor(Math.random() * 100000) / 100000;
}
System.out.print(values[i][j] + " ; ");
}
System.out.println();
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论