2

I am able to generate Random Numbers with Random method. But I want to create random numbers that are displayed in a binary format. The numbers must be within a specific range.

Random numbers should be between 5 and 10 digits, inclusive.

E.g.: 011110, 0111100, 010110101.

ZygD
  • 22,092
  • 39
  • 79
  • 102
Shreenivas
  • 155
  • 2
  • 6
  • 19
  • Try this: [Generating random integers in a range with Java][1] [1]: http://stackoverflow.com/questions/363681/generating-random-integers-in-a-range-with-java – randymay Sep 04 '14 at 14:34
  • i know to create random numbers . but unable to give specific range and specific digits – Shreenivas Sep 04 '14 at 14:35
  • 1
    the requirements as i read them [digits must be 0 or 1, digits must not be reused, there must be at least 5 digits] are not possible in union. this is probably just a phrasing issue, but your question is unclear. did you mean that generated numbers should not repeat? – Woodrow Barlow Sep 04 '14 at 14:36
  • should leading zeroes be counted towards the "length" of the number (for example, would `01111` be considered 4 or 5 digits long?) – Woodrow Barlow Sep 04 '14 at 14:38
  • @WoodrowBarlow 5 digits – Shreenivas Sep 04 '14 at 14:40
  • If you're trying to generate binary numbers, it's probably just easier to say that. – Powerlord Sep 04 '14 at 14:41
  • yes want to create binary numbers. – Shreenivas Sep 04 '14 at 14:42
  • Do you understand that numbers in Java are not stored in a particular format (like binary)? It sounds like what you actually need is a method which converts a number into a particular display format (e.g. a `String` which displays a binary representation of an `int`). – azurefrog Sep 04 '14 at 14:44
  • @azurefrog ya I understood. i am going to store this numbers in database so there i can check it . but want to prevent before inserting in the database. – Shreenivas Sep 04 '14 at 14:46
  • In what format are you going to store it? Prevent *what* before inserting it? – azurefrog Sep 04 '14 at 14:48
  • OK i understood the problem. so now at least how can generate random binary numbers. – Shreenivas Sep 04 '14 at 14:50

5 Answers5

5

Try the Following Code Snippet (Random number generation from This Stack Overflow question)

import java.util.Random;

public class Main {
    private static Random random = new Random();
    private static final int MAX = 1023;
    private static final int MIN = 16;

    public static void main(String[] args) {
        Integer randInt = random.nextInt((MAX - MIN) + 1) + MIN;
        System.out.println("random binary is: " + Integer.toBinaryString(randInt));
    }
}

MIN of 16 enforces a minimum of 5 binary digits where:

10000 is equal to 16

MAX of 1023 enforces a max of 10 binary digits where:

111111111 equal to 1023

The Utility method Integer.toBinaryString(int) converts the Integer into a binary readable format (enforcing the only '1's and '0's condition)

Community
  • 1
  • 1
Blake Yarbrough
  • 2,286
  • 1
  • 20
  • 36
  • 2
    `11111` is not 63, it's 31, and `10000` is 5 digits too and is 16. So `MIN = 16;` would probably make more sense. Similarly, MAX should be 1023. – assylias Sep 04 '14 at 15:01
  • yes at last i made it . MAX=999 and MIN=16.Its working perfectly – Shreenivas Sep 04 '14 at 15:06
  • thank assylias, I have edited it per your suggestions to make it more correct. – Blake Yarbrough Sep 04 '14 at 15:07
  • 2
    this solution will never find numbers with leading zeroes. additionally, it makes no attempt to avoid duplicate numbers. – Woodrow Barlow Sep 04 '14 at 15:09
  • @WoodrowBarlow It is not clear based on the question if leading zeroes matter or not and the question does not specify that numbers should not repeat... – assylias Sep 04 '14 at 15:16
  • 2
    the question was edited to remove the duplication stipulation, and the author stated in a comment that zeroes were significant (see the comments above). but it seems that it wasn't actually important, so that's okay. – Woodrow Barlow Sep 04 '14 at 15:18
1
private Set set = new LinkedHashSet();
private Random rand = new Random();
public String getNo(){
    int range = getRandom(5, 10);        
    StringBuffer nu = new StringBuffer();
    for(int i=0,length=range;i<length;i++){
        nu.append(getRandom(0, 1));
    }
    String nuString = nu.toString();
    if(set.contains(nuString)){
        return getNo();
    }
    set.add(nuString);
    return nuString;
}

public int getRandom(int min, int max) {            
    return rand.nextInt((max - min) + 1) + min;
}
Raja CSP
  • 172
  • 7
1
import java.util.Random;

public class RandomNumber {
    private static Random random = new Random();
    private static final int maximumDigits = 1023;
    private static final int minimumDigits = 16;

    public static void main(String[] args) {
        Integer randInt = random.nextInt((maximumDigits - minimumDigits) + 1) + minimumDigits;
        System.out.println("random binary is: " + Integer.toBinaryString(randInt));
    }
}

Its a response to LanguidSquid program, I can't comment to his post for <50 reps.

Try this one. It should be limited from 5 digits to 10 digits. Because decimal 16 equivalent to 10000 and 1023 is equivalent to 1111111111 in binary.

Tarek
  • 771
  • 7
  • 18
1

Here is a brief program that accomplishes exactly what you've requested (although perhaps not what you actually want). It should be noted that storing the values in Strings is necessary because you specified in the comments that leading zeroes are possible and must count towards the length of the number.

This program will generate five (you can increase this) Strings which will be anywhere inclusively between 5 and 10 characters long. Any of those characters can be arbitrarily zero or one. The list of all Strings is guaranteed not to contain any duplicates.

import java.util.Random;

public class Test {

    /* generate a random number, inclusively */
    public static int randNum(int min, int max) {
        Random rand = new Random();
        int randomNum = rand.nextInt((max - min) + 1) + min;
        return randomNum;
    }

    public static boolean contains(String haystack[], String needle) {
        for(int i=0; i<haystack.length; i++)
            if(haystack[i].equals(needle)) return true;
        return false;
    }

    public static void main(String []args){
        String numbers[] = {"","","","",""};
        int i = 0;
        while(i < 5) {
            int length = randNum(5,10);
            String number = "";
            for(int j=0; j<length; j++) {
                number += String.valueOf(randNum(0,1));
            }
            if(contains(numbers,number)) continue;
            else {
                numbers[i] = number;
                i++;
            }
        }

        for(int k=0; k<numbers.length; k++) {
            System.out.println(numbers[k]);
        }
    }
}
Woodrow Barlow
  • 8,477
  • 3
  • 48
  • 86
  • avoiding duplicates was *originally* a requirement of this problem statement. – Woodrow Barlow Sep 04 '14 at 15:11
  • 1
    seriously?? You came up with this answer after that 5 line answer of Languid? – Raja CSP Sep 04 '14 at 15:12
  • OP originally said that a list of random numbers must be generated and that the list can contain no duplicates. OP also stated in a comment that leading zeroes should be possible and should count towards the digit count. The accepted solution fails to meet either requirement. – Woodrow Barlow Sep 04 '14 at 15:15
  • Your answer is perfect and excellent but i got the answer before your post. – Shreenivas Sep 04 '14 at 15:19
1

http://xkcd.com/221/

Here you would have the Java snippet:

public static int getRandomNumber() {
    return 4; // Chosen by fair dice roll. Guarantied to be random.
}
gmanolache
  • 497
  • 4
  • 12