Java BitSet pickRandomSetIndexFromBitSet(BitSet bitset)

Here you can find the source of pickRandomSetIndexFromBitSet(BitSet bitset)

Description

pick Random Set Index From Bit Set

License

Open Source License

Declaration

public static int pickRandomSetIndexFromBitSet(BitSet bitset) 

Method Source Code


//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());
    }
}

Related

  1. not(BitSet bitSets)
  2. or(BitSet bs1, BitSet bs2)
  3. pack(BitSet bitSet)
  4. padToByteBoundary(BitSet actualParameter, int sizeInBits, int byteBoundLength, boolean preserveFirstAsSign)
  5. parseBitSet(byte[] sfData, int offset, int length)
  6. printBitSet(BitSet bs)
  7. printBitSet(BitSet iBits, int length)
  8. readByte(BitSet bits, int startByte)
  9. reverse(final BitSet bitset, final int sizeInBits)