Here you can find the source of pickRandomSetIndexFromBitSet(BitSet bitset)
public static int pickRandomSetIndexFromBitSet(BitSet bitset)
//package com.java2s; //License from project: Open Source License import java.util.BitSet; public class Main { public static int pickRandomSetIndexFromBitSet(BitSet bitset) { if (bitset.isEmpty()) { throw new RuntimeException("The bitset is empty, cannot find a set element"); }/*from w w w . j ava 2 s. c o m*/ // Generate list of set elements in the format that follows: { 2, 4, 5, ...} String set = bitset.toString(); // Separate the elements, and pick one randomly String[] indexes = set.substring(1, set.length() - 1).split(","); return Integer.parseInt(indexes[(int) (Math.random() * (indexes.length - 1))].trim()); } }