I have two BitSets which have to be initialized randomly with the length of 20 bits.
I am trying to achieve that by initializing the BitSets with 20 bits each and within a for loop iterating through the BitSets and call nextBoolean() of the Random class. However, the length is not always 20. So, I have been playing around with it, and figured it that it might be because it does not count the false bits as part of the length. In case I understand it correctly, how do I force it to have 20 random bits always?
public static void generate() {
BitSet set1 = new BitSet(20);
BitSet set2 = new BitSet(20);
Random r = new SecureRandom();
for (int i = 0; set1.length() < 20 && set2.length() < 20; i++) {
set1.set(i, r.nextBoolean());
set2.set(i, r.nextBoolean());
}
StringBuilder s = new StringBuilder();
for (int i = 0; i < set1.length(); i++) {
s.append(temp1.get(i) == true ? 1 : 0);
}
System.out.println(s + " " + s.length() + " " + set1.length() + " "+ set2.length());
}
Thanks in advance.