设为首页 加入收藏

TOP

运用简单的bloomfilter算法生成100万个不重复的随机数
2015-07-24 06:29:12 来源: 作者: 【 】 浏览:33
Tags:运用 简单 bloomfilter 算法 生成 100万 重复 随机

本文中只是简单的体会bloomFilter算法的基本原理,设计实现一个生成100万个不重复的随机数。

选择3个分布均匀质数,在这里面质数的选择还是挺有讲究的,要注意不能太小,必须能够满足bloomfilter空间,不然整个空间都是1了还没有找到100万个不重复的随机数。不多说,上代码。

#include
  
   
#include
   
     #include
    
      #include
     
       #define MAXNUM 10000000 int hash_fuction(int dst, int select_number) { return dst % select_number; } int * byte_bloomfilter_random(int generate_number, int maxValue) { int temp; char * bloomfilter; int *dst; bool flag; int index_a, index_b, index_c; char diff_a, diff_b, diff_c; bloomfilter = (char *)malloc((size_t)MAXNUM / 8 * sizeof(char)); dst = (int *)malloc((size_t)generate_number * sizeof(int)); for (int i = 0; i < MAXNUM / 8; i++) { bloomfilter[i] = 0; } for (int i = 0; i < generate_number; i++) { flag = true; while (flag) { int temp_a, temp_b, temp_c; char bit_a, bit_b, bit_c; temp = rand() * rand() % maxValue; //select 3 prime numbers and select 3 hash functions temp_a = hash_fuction(temp, 524287); temp_b = hash_fuction(temp, 1046527); temp_c = hash_fuction(temp, 3967); index_a = temp_a >> 3; diff_a = temp_a % 8; index_b = temp_b >> 3; diff_b = temp_b % 8; index_c = temp_c >> 3; diff_c = temp_c % 8; bit_a = bloomfilter[index_a] & (1 << diff_a); bit_b = bloomfilter[index_b] & (1 << diff_b); bit_c = bloomfilter[index_c] & (1 << diff_c); if (!bit_a || !bit_b || !bit_c) { dst[i] = temp; bloomfilter[index_a] = bloomfilter[index_a] | (1 << diff_a); bloomfilter[index_b] = bloomfilter[index_b] | (1 << diff_b); bloomfilter[index_c] = bloomfilter[index_c] | (1 << diff_c); flag = false; } } } free(bloomfilter); return dst; }
     
    
   
  


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇POJ 2356 Find a multiple 鸽巢原.. 下一篇BZOJ 刷题记录 PART 2

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: