(例子)无重复随机数生成器
来源:互联网 发布:cdx什么意思网络用语 编辑:程序博客网 时间:2024/06/05 03:03
#ifndef URAND_H
#define URAND_H
#include<bitset>
#include<cstddef>
#include<cstdlib>
#include<ctime>
#include<iostream>
using std::size_t;
using std::bitset;
using namespace std;
template<size_t UpperBound>class Urand
{
bitset<UpperBound> used;
//..bitset<n> b;
//..b有n位,每位都为0.参数n可以为一个表达式.
//..如bitset<5> b0;则"b0"为"00000"
public:
Urand(){srand(time(0));}
size_t operator()();
};
template<size_t UpperBound>
inline size_t Urand<UpperBound>::operator()()
{
if(used.count()==UpperBound)
used.reset();
size_t newval;
while(used[newval=rand()%UpperBound])
;
used[newval]=true;
return newval;
}
#endif
int main()
{
Urand<10>u;
for(int i=0;i<10;i++)//产生10以内的无重复数据
cout<<u()<<",";
cout<<"\n10个以内无重复..."<<endl;
for(int i=0;i<20;i++)
cout<<u()<<",";
return 0;
}
#define URAND_H
#include<bitset>
#include<cstddef>
#include<cstdlib>
#include<ctime>
#include<iostream>
using std::size_t;
using std::bitset;
using namespace std;
template<size_t UpperBound>class Urand
{
bitset<UpperBound> used;
//..bitset<n> b;
//..b有n位,每位都为0.参数n可以为一个表达式.
//..如bitset<5> b0;则"b0"为"00000"
public:
Urand(){srand(time(0));}
size_t operator()();
};
template<size_t UpperBound>
inline size_t Urand<UpperBound>::operator()()
{
if(used.count()==UpperBound)
used.reset();
size_t newval;
while(used[newval=rand()%UpperBound])
;
used[newval]=true;
return newval;
}
#endif
int main()
{
Urand<10>u;
for(int i=0;i<10;i++)//产生10以内的无重复数据
cout<<u()<<",";
cout<<"\n10个以内无重复..."<<endl;
for(int i=0;i<20;i++)
cout<<u()<<",";
return 0;
}
0 0
- (例子)无重复随机数生成器
- 无重复随机数
- Matlab 产生无重复随机数
- 生成无重复的随机数
- C#产生无重复随机数
- 生成无重复的随机数
- 生成无重复的随机数
- java 无重复取随机数
- java 无重复取随机数
- 随机数生成器(笔记)
- 例子---随机数/不重复的随机数
- 谈谈无重复随机数的产生方法
- 谈谈无重复随机数的产生方法
- 无重复随机数的产生方法
- asp.net 无重复随机数代码
- Swift - 产生不重复数字的随机数生成器
- 随机数生成器
- 随机数生成器
- nyoj-449-不再爱你……
- 架构师如何从复杂中设计出简单? – 反思许多架构师的迷思
- dell inspiron7420 ubuntu13.04触摸板设置
- OnScrollListener
- oracle 11g安装图解
- (例子)无重复随机数生成器
- IOS6上label文字空白
- 常用awk命令(转)
- WINCE6.0+S3C6410的触摸屏驱动
- 【Qt编程】Qt学习之Window and Dialog Widgets
- hdu 1421搬寝室(简单dp)
- node.js 之事件驱动
- Pat(Advanced Level)Practice--1066(Root of AVL Tree)
- 标准c++中string类介绍