English 中文(简体)
Rand比特
原标题:Randomly pick k bits out of n from a Java BitSet

如何从http://docs.oracle.com/javase/1.4.2/docs/api/java/util/BitSet.html> rel=“nofollow noreferer”>Java BirSet of lengthm with bits,

Example input: m=20, n=11 enter image description here

Example output: k=3 enter image description here

The naive approach

选择随机编号0≤ i ≤ m-1. 如果该编号在投入上转而没有在产出上转,则该编号在产出中转至k bits在产出中转手。

This approach fails when n is much smaller than m. Any other ideas?

最佳回答

您可从第1轨到最后一行,并将reservoir symposium应用于设定的轨道。

The algorithm has O(m) time complexity, and requires O(k) memory.

问题回答

How about finding n positions of all set bits and placing them in a collection as the first step, and them choosing k positions from that collection randomly?

如果制约因素允许你通过:

持有所有固定轨道指数的<代码>List。 http://www.ohchr.org。 选择第一个<代码>k的指数,从填充的名单中计算。

http://www.ohchr.org。 根据这些评论,如果<代码>k实际上很小,而n则该算法可能无效。 此处为一种选择:生成<代码>k随机、不同编号的<代码>[0, n]。 如果在一组选定的指数中已经存在一些指数,则采用链式办法:即,在获得该套指标中尚未出现的数字之前,将数字增加1。 最后,所生成的指数是你选择的参数。

If n is much larger than k, you can just pare down the Fisher-Yates shuffle algorithm to stop after you ve chosen as many as you need:

private static Random rand = new Random();
public static BitSet chooseBits(BitSet b, int k) {
    int n = b.cardinality();
    int[] indices = new int[n];
    // collect indices:
    for (int i = 0, j = 0; i < n; i++) {
        j=b.nextSetBit(j);
        indices[i] =j++;
    }
    // create returning set:
    BitSet ret = new BitSet(b.size());
    // choose k bits:
    for (int i = 0; i<k; i++) {
        //The first n-i elements are still available.
        //We choose one:
        int pick = rand.nextInt(n-i);
        //We add it to our returning set:
        ret.set(indices[pick]);
        //Then we replace it with the current (n-i)th element
        //so that, when i is incremented, the 
        //first n-i elements are still available:
        indices[pick] = indices[n-i-1];
    }
    return ret;
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签