2D数组具有随机值,但是两列中的值不应相同。

huangapple go评论79阅读模式
英文:

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 &lt; values.length; i++) {
for (int j = 0; j &lt; values[i].length; j++) {
double x = Math.random();
double y = x &lt; 0.25 ? 1 : x &lt; 0.5 ? 0.74 : x &lt; 0.75 ? 0.49 : 0.01;
if (j == 0) {
values[i][j] = Math.random() &lt; 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 &lt; 0.25 ? 1 : x &lt; 0.5 ? 3 : x &lt; 0.75 ? 7 : 20; 
} else if (j == 4) {
values[i][j] = y &lt; 0.25 ? 1 : y &lt; 0.5 ? 3 : y &lt; 0.75 ? 7 : 20;
} else {
values[i][j] = Math.floor(Math.random() * 100000) / 100000; 
}
System.out.print(values[i][j] + &quot; ; &quot;);
}
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 &lt; values.length; i++) {
List&lt;Integer&gt; possibleInts = new ArrayList&lt;Integer&gt;(Arrays.asList(1, 3, 7, 20));
for (int j = 0; j &lt; values[i].length; j++) {
if (j == 0) {
values[i][j] = Math.random() &lt; 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] + &quot; ; &quot;);
}
System.out.println();
}
}
}

huangapple
  • 本文由 发表于 2020年10月15日 16:00:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/64367238.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定