0

I have code that randomly generates a binary number from 000 to 111 but I'm having trouble having it create a number from 000 to just 110. I know that I can somehow rerun the code everything it comes out with 111 but I can't seem to figure out the way to have it do that.

public String binNumber() {
        StringBuilder storage = new StringBuilder();
        int i = 0;
        while (i < 3) {
            int binny = this.giveMeBinary();
            storage.append(String.valueOf(binny));

            i++;
        }


        return storage.toString();
    }

public int giveMeBinary() {
        Random rg = new Random();
        int bin = rg.nextInt(2);
        return bin;

    }
1
  • 2
    You are generating binary digits which appear to be the problem, try generating an integer number from 0 to 6 and then convert it to binary. Feb 1, 2012 at 5:55

5 Answers 5

8

The better way to do this is to generate a random number from 0 to 6 inclusive, and then convert to a string

public String binNumber() {
    Random rg = new Random();
    int n = rg.nextInt(7);
    return Integer.toBinaryString(n);
}
1
  • I think you mean the second line should take 7 as the argument.
    – Dawood
    Feb 1, 2012 at 5:58
3

"Rerolling" if you get 111 is definitely a bad idea; that's potentially an algorithm with infinite running time, although in practice it would do quite well.

Why work in base 2? Work in base 10, easily get a random number between 0 and 6, and then translate back to binary.

3
  • Part of my project is to do it in binary.
    – willkara
    Feb 1, 2012 at 5:57
  • 1
    I disagree that this is a bad idea. The probability of rerolling is 1/8 on each iteration, so with probability 7/8 you will terminate on each iteration. This gives an expected number of iterations of 8/7, with the probability of more iterations decreasing exponentially. It would be an extremely rare event for this to require more than ten or so iterations, since that has probability 1/(8^10) = 1 / (2^30), which is about one in a billion. The likelihood that this takes more than one hundred iterations is so low in the entire history of the universe it's unlikely to ever happen. Feb 1, 2012 at 5:59
  • 1
    Like I said, "in practice it would do quite well." Surely you can agree that we should try to stay away from O(infinity) algorithms when there are sensible alternatives.
    – Jeremy
    Feb 1, 2012 at 6:00
1

Something like:

Random random = new Random();

int nextNumber = random.nextInt( 7 );
System.out.println( Integer.toBinaryString( nextNumber ) );

Remember your numbers you create in a computer are always in binary. You just need to print it out differently. So even though you've given it a decimal number that number is converted by the compiler to binary. 7 == 0x111 when its compiled.

1

If you want to re-roll, try this:

String num = "";
while (true)
{
      num = binNumber();
      if (!num.equals("111"))
      {
          break;
      }
}
return num; 
0

I created a method which can return a random binary number below 128 (value 128 can be alter in the code)-

    public static void RandomBinary(int len){
    if(len<128) {
        Random random = new Random();
        boolean res;
        String randomBinary = "";
        for (int i = 0; i < len - 1; i++) {
            res = random.nextBoolean();
            if (res)
                randomBinary = randomBinary.concat("1");
            else
                randomBinary = randomBinary.concat("0");
        }
        System.out.print(randomBinary);
    }else {
        System.out.print("KeyGen maximum length allowed is 128. Please enter value below 128");
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.