更优雅的方式选择比特大小和伪随机比特生成

时间:2012-12-25 18:59:53

标签: c# random binary bit-manipulation

有没有比我在这里做的更优雅的方法来进行位大小选择和伪随机二进制位生成?实际上我需要编写一种算法,用户可以控制生成的随机二进制位的位大小(最大16位)。这是我写的功能,但我不确定这是否是最小/最优雅的。由于这是科学的需要,效率并不重要,但代码的优雅和易于理解确实很重要。那么有更高效/优雅的方式做同样的事情吗?

static string randomBit() {
    int bitSize = 0, input = 0;
    Console.Write("Input Bit Size (Maximum is 16 Bit): ");
    input = Convert.ToInt32(Console.ReadLine());
    Random choice = new Random();
    if(input == 0 || input > 16) {
        bitSize = 0;
    }
    else if(input == 1) {
        bitSize = 1;
    }
    else if(input == 2) {
        int randomChoice = choice.Next(2, 3);
        bitSize = randomChoice;
    }
    else if(input == 3) {
        int randomChoice = choice.Next(4, 7);
        bitSize = randomChoice;
    }
    else if(input == 4) {
        int randomChoice = choice.Next(8, 15);
        bitSize = randomChoice;
    }
    else if(input == 5) {
        int randomChoice = choice.Next(16, 31);
        bitSize = randomChoice;
    }
    else if(input == 6) {
        int randomChoice = choice.Next(32, 63);
        bitSize = randomChoice;
    }
    else if(input == 7) {
        int randomChoice = choice.Next(64, 127);
        bitSize = randomChoice;
    }
    else if(input == 8) {
        int randomChoice = choice.Next(128, 255);
        bitSize = randomChoice;
    }
    else if(input == 9) {
        int randomChoice = choice.Next(256, 511);
        bitSize = randomChoice;
    }
    else if(input == 10) {
        int randomChoice = choice.Next(512, 1023);
        bitSize = randomChoice;
    }
    else if(input == 11) {
        int randomChoice = choice.Next(1024, 2047);
        bitSize = randomChoice;
    }
    else if(input == 12) {
        int randomChoice = choice.Next(2047, 4095);
        bitSize = randomChoice;
    }
    else if(input == 13) {
        int randomChoice = choice.Next(4096, 8191);
        bitSize = randomChoice;
    }
    else if(input == 14) {
        int randomChoice = choice.Next(8192, 16383);
        bitSize = randomChoice;
    }
    else if(input == 15) {
        int randomChoice = choice.Next(16384, 32767);
        bitSize = randomChoice;
    }
    else if(input == 16) {
        int randomChoice = choice.Next(32768, 65535);
        bitSize = randomChoice;
    }
    string binary = Convert.ToString(bitSize, 2);
    return binary;
}

另外,作为第二个问题,如果我按两次Enter键,当代码询问位大小时,会返回异常错误。有没有办法绕过同样的?

1 个答案:

答案 0 :(得分:0)

首先,我真的认为你可以减少if / else语句的数量:

private static string randomBit()
{
    int bitSize = 0, input = 0;
    Console.Write("Input Bit Size (Maximum is 16 Bit): ");
    input = Convert.ToInt32(Console.ReadLine());
    Random choice = new Random();

    if (input <= 0 || input > 16)
    {
        bitSize = 0;
    }
    else if(input == 1)
    {
        bitSize = 1;
    }
    else
    {                        
        int randomChoice = choice.Next(1 << (input-1), (1 << input)-1);
        bitSize = randomChoice;
    }

    string binary = Convert.ToString(bitSize, 2);
    return binary;
}

如果您对此表达式中的左移不满意:int randomChoice = choice.Next(1 << (input-1), (1 << input)-1);并且您对可读性更感兴趣,那么您可以随时将其替换为:

int randomChoice = choice.Next(Math.Pow(2, input - 1), Math.Pow(2, input) - 1);

关于例外的问题,答案是肯定的。您遇到的问题是您没有验证Convert.ToInt32功能的输入。

而不是input = Convert.ToInt32(Console.ReadLine());你可以写:

do
{
   string inputString = Console.ReadLine();
   if(!Int32.TryParse(inputString, out input))     // will return false if it can't convert
   {
       Console.WriteLine("Please enter a number between 1 and 16!");
       input = 0;
   }
}while(input == 0);