K distinct integers in [0, N], select a random number between 0 and N which is not in the K list. 多谢
f*x
2 楼
前几天下载KMPLAYER的副产品,太TMD烦人。
g*j
3 楼
怎么做?
which
【在 l**********n 的大作中提到】 : K distinct integers in [0, N], select a random number between 0 and N which : is not in the K list. : 多谢
f*x
4 楼
up
z*8
5 楼
I think this requires pre-processing to map all K integers to last K integers in the array which takes O(K) time. After that, you can simply do Rand%(N-K) to get the number which is not in the K in constant time
l*n
6 楼
具体怎么做? 假如不能用额外的data structure, 还有其他更好的办法么?
【在 z*********8 的大作中提到】 : I think this requires pre-processing to map all K integers to last K : integers in the array which takes O(K) time. : After that, you can simply do Rand%(N-K) to get the number which is not in : the K in constant time
k*4
7 楼
如果可以改变数组的话。first positive integer 抽样可行不。
K distinct integers in [0, N], select a random number between 0 and N which is not in th........
【在 l**********n 的大作中提到】 : K distinct integers in [0, N], select a random number between 0 and N which : is not in the K list. : 多谢
j*6
8 楼
没看懂 为什么要 map all K integers to last K integers in the array. 为什么最后只需要Rand%(N-K)? 如何N = 10, K array 是 0-7, K= 8, 那么你最终只 能随机出0 1 2 三个随机数,永远都是在array 的范围内? 不太明白 求解答 多谢啦
【在 z*********8 的大作中提到】 : I think this requires pre-processing to map all K integers to last K : integers in the array which takes O(K) time. : After that, you can simply do Rand%(N-K) to get the number which is not in : the K in constant time
这K个intergers应该是已知的吧?那这样可不可以? int generate(int *kList, int K, int N) { int num; gen: num = rand()*N; for (int i = 0; i < K, i++) { if num == kList[i]; goto gen; } return num; }
which
【在 l**********n 的大作中提到】 : K distinct integers in [0, N], select a random number between 0 and N which : is not in the K list. : 多谢
k*4
13 楼
这就是随机算法了,稍稍改进,可以二分查找num是否在kList中。
【在 f*****2 的大作中提到】 : 这K个intergers应该是已知的吧?那这样可不可以? : int generate(int *kList, int K, int N) : { : int num; : gen: : num = rand()*N; : for (int i = 0; i < K, i++) : { : if num == kList[i]; : goto gen;
这个题和reservior sampling 是什么关系呢? 假如 N =100, K=10 reservior sampling是说,a[0]-a[9]就是前10个数,从a[10]开始,generate a random number j in N, if j < k, 替换, 否则skip到下一个数。 这个题的意思是不是说, a[0]- a[9] 还是1-k这些数,从a[10]开始,generate a random number j in N, if j > k, return the number, else skip to next number. 和reservior sampling条件相反,但意思是一样的?