Reputation: 457
public class Main {
public static void main(String[] args) {
// generate a random String of 0's and 1's that is 8 digits long
System.out.println("Print the string of 0's and 1's");
}
}
I want to do it in just one, and only one line of code.
Upvotes: 0
Views: 1695
Reputation: 719376
Here's another "solution":
System.out.println((Integer.toBinaryString(
new Random().nextInt()) + "00000000").substring(0, 8));
... except that it is slightly biased. Line wrapped for readability!
Also, note that according to a literal interpretation:
{ int tmp = new Random().nextInt(); /* add more statements here */ }
is one line of code. And it is even one Java statement :-).
UPDATE: here's another version with no bias:
System.out.println(Integer.toBinaryString(
new Random().nextInt() | 0x80000000).substring(24));
Forcing the sign bit to be 1 means that we won't have to worry about whether the leading zeros are there or not.
Upvotes: 0
Reputation: 11579
You can use RandomStringUtils and specify which characters you need.
System.out.println(RandomStringUtils.random(8, '0', '1'));
Upvotes: 2
Reputation: 2917
In case you don't want to include binary numbers starting with 0 like '00001010' and you only want to represent binary number that starts with 1 then you 'play' between 128 and 255, cause the 10000000 = 128 in decimal and 11111111 = 255 in that case you will need to do something like this:
System.out.println(Integer.toBinaryString(128 + (int) (127 * Math.random())));
The Math.random() gives you a number bewteen 0.0 and 1.0 so 127 * Math.random will give you a random value between [0,127] and because we want to start our binary number with '1' then for sure the number must be bigger that 128 = 10000000.
In case you want to include all the number from 0 to 255 here is what you could do :
System.out.println(String.format("%8s", Integer.toBinaryString((int) (255 * Math.random()))).replace(' ', '0'));
Upvotes: 2
Reputation: 33471
Just another solution:
String.format("%8s", Integer.toBinaryString(new Random().nextInt() & 0xFF)).replace(' ', '0')
No streams, single Random
, but with String
manipulations...
Upvotes: 0
Reputation: 53535
We can randomly choose a number that has 8 bits in it's representation and print it:
int n = ThreadLocalRandom.current().nextInt(128, 256);
System.out.println(Integer.toBinaryString(n));
we want the number to be bigger than 127 because otherwise we won't have 8 bits (for the same reason we want it to be smaller than 256).
Upvotes: -1
Reputation: 1874
You can also do it with an IntStream
. Its not a great solution because it instantiates several Random
objects, but its one line of code:
IntStream.rangeClosed(1, 8).forEach(x->System.out.print(new Random().nextInt(2)));
Upvotes: 2
Reputation: 2907
This is one line, though it's definitely not the most readable way of doing this. Most things are possible in one line, but not easy to debug or understand at a glance. This also isn't the fastest way, but you said you didn't care about efficiency.
String random8Bits = new Random().ints(8, 0, 2).mapToObj(Integer::toString).collect(Collectors.joining());
How it works:
new Random()
// creates a random number generator
.ints(8, 0, 2)
// returns an IntStream containing 8 random numbers between 0 (inclusive) and 2 (exclusive).
.mapToObj(Integer::toString)
// converts each int in the stream to its string representation
.collect(Collectors.joining())
// joins the strings
For further information, see Random.ints(long, int, int)
, IntStream
and Collectors.joining()
.
Upvotes: 1
Reputation: 365
You want to use the toBinaryString method of Class Integer. Then pass it a new random it wich you short to a byte (8 bits) by applying a & 0xff.
import java.util.*;
public class Main {
public static void main(String[] args) {
System.out.println(Integer.toBinaryString(new Random().nextInt() & (0xff)));
}
}
Upvotes: 0