Reputation: 155
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.
Upvotes: 2
Views: 4209
Reputation: 2316
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)
Upvotes: 5
Reputation: 497
Here you would have the Java snippet:
public static int getRandomNumber() {
return 4; // Chosen by fair dice roll. Guarantied to be random.
}
Upvotes: 1
Reputation: 771
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.
Upvotes: 1
Reputation: 9107
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]);
}
}
}
Upvotes: 1
Reputation: 172
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;
}
Upvotes: 1