0

这是我到目前为止的鳕鱼:

public class Laboratoire05E {
public static void main(String[] args) {

    int number = 0;
    int counter = 0;

     do {
        bin = (int) (Math.random()*2);
        System.out.print(bin);
        counter++;

        if(counter>=80) {
            System.out.print("\n");
            counter = counter-80;
            }
        } while (bin < 2);
}

}

控制台的输出如下:

01000111000011101011010011011000000100100110100101011011010100111011111110110111 00101100001001100010001100101001101110011000110110111010100010011111000101110011 00000010111101011110100100100100000101001111000110001101010011000001110100000011 10100001001011100110100110010011100110001110100111111110111101111000010100001110 11011001110001101111110111010111111110100110100000100001011100011100011001000101 01001111111011001101000010111010111010111100001101010001100001101011111010001111 00110010110101100010000101001111011111

这会无限期地持续下去。

4

4 回答 4

7

请记住,您的循环可能会连续找到 10 个 0 (.5^10) = 0.0977 %。如果您增加该数量,您可能需要额外检查何时counter超过生成的数字数量的某个阈值。

int zeroCount = 0
do {
    bin = (int) (Math.random()*2);
    System.out.print(bin);
    counter++;

    // Check for 10 in row:
    if(bin == 0) {
        zeroCount ++;
    } else {
        zeroCount = 0;
    }
    if(zeroCount == 10) {
        break;  // Exit loop
    }

    if(counter>=80) {
        System.out.print("\n");
        counter = counter-80;
        }
} while (bin < 2);
于 2012-10-12T19:02:52.337 回答
5

创建一个Random可以稍微更有效

Random rand = new Random();
for (int i = 1; i <= 10; i++) {
    int bin = rand.nextInt(2);
    System.out.print(bin);
    if (bin == 1) i = 0;
}

打印例如:

111101001011001001011000111100010110011001011111011100001110100011100100100101101011110110111101101010110111000000110111011010111001101111010100111011000000101011000100000010001001010010011100100011111100010100011101101111100100111110110101101010111010001111111010000101010011100110111100010001100011011011101001000010001001010010110000011000001011101001110001111110000101101100011100111110000010000010001100000001011110110000110000001000100010001010000111001111111111100101101111110101011000010101110000110010011111001100100010111001001010110100001111011001000101011101101010110110010010001000001011010101000001000101011001000101000001111100111011110111101100010010110100001111011111110000011001010111100101100100110101101101000011100101011010111001110011110011110010001110001111001101000001111001001100101101010111101000000101011011110100001001100000111100000101111010111011000010110011001010011000001001101001100011101100001000100110101011000100010010001011100100100010000111111010001010000001101011010101101001000110110011110000100101100100000000101001100011110110000000000

于 2012-10-12T19:14:58.270 回答
2

逐位解决方案很有趣...

int mask = 0x3FF; // masks last 10 bits
int buffer = mask; // initialize buffer to all ones

while ((mask & buffer) != 0) {
    int digit = (int) (Math.random() * 2);
    buffer = (buffer << 1) | digit;
}
于 2012-10-12T19:24:00.933 回答
2
  1. 在循环外将计数器变量初始化为零
  2. 如果随机结果为 0,则递增计数器,否则将计数器重置为 0
  3. 如果计数器 == 10,则中断
于 2012-10-12T19:08:41.160 回答