[C++] Rander

注 这个Rander对单个数据的平均分散不太优秀,但是获取大量数据十分平均

当前版本 2.0 for Windows

功能

int rander::reset() 按默认大小重置随机数序列,返回默认大小. (自动检测更新,无需手动).

int rander::reset(int size)\(size\) 重置随机数序列,返回重置大小,失败返回 \(-1\).

vector<long long> rander::randlist(int size) 返回一个 \(size\) 大小的随机数序列,范围为 \([0,2^{32}-1]\).

int rander::rand() 返回一个随机数,范围为 \([-2^{31},2^{31}-1]\).

int rander::rand(int mod) 返回一个随机数,范围为 \([-mod,mod-1]\). 最大范围为 \([-2^{31},2^{31}-1]\)

long long rander::randabs(int mod) 返回一个随机数,范围为 \([0,mod-1]\). 最大范围为 \([0,2^{32}-1]\).

long long rander::rand(int l,int r) 返回一个随机数,范围为 \([l,r]\),最大范围为 \([0,2^{32}-1]\).

T from(vector<T> p) 从给定任意列表中随机返回一个.

void randsort(vector<T> &p) 把给定的任意列表打乱.

void randsort(T &p,int from,int to) 把给定的任意数组的 \([from,to]\) 下标内的元素随机打乱.

代码

#include<bits/stdc++.h>
using namespace std;
namespace rander{
	vector<long long> list;
	int it,ssize;
	int reset(int size){if(size<=0)return -1;int seed=std::rand();mt19937 Rand(seed);ssize=size;it=0;list.clear();for(int i=1;i<=size;++i){list.push_back(Rand());}return seed;}
	int reset(){struct _timeb T;_ftime(&T);srand(T.millitm);return rander::reset(1000);}
	vector<long long> randlist(int size){int seed=ssize;reset(size);vector<long long> ret=list;reset(seed);return ret;}
	int rand(){if(ssize<=0)reset();int ret=list[it];it++;if(it>=ssize)reset(ssize);return ret;}
	int rand(int mod){int ret=rander::rand()%mod;return ret;}
	long long randabs(int mod){long long ret=abs(rander::rand());if(mod)ret%=mod;return ret;}
	long long rand(int l,int r){if(l<0)return rander::rand(0,r);long ret=rander::randabs(r-l+1)+l;return ret;}
	template<typename _T>_T from(vector<_T>p){long long ret=rander::randabs(p.size());return p[ret];}
	template<typename _T>void randsort(vector<_T>&p){for(int i=1;i<=p.size();++i){int x=rander::rand(0,p.size()-1),y=rander::rand(0,p.size()-1);swap(p[x],p[y]);}}
	template<typename _T>void randsort(_T &p,int from,int to){for(int i=1;i<=to-from+1;++i){int x=rander::rand(from,to),y=rander::rand(from,to);swap(p[x],p[y]);}}
};

使用例

获取随机数

using namespace rander;
int a=rand(114514);
int x=rand(1,114514);

随机选取内容

using namespace rander;
vector<string> x{"e","k"};
cout<<from(x)<<endl;
cout<<from<string>({"e","k"})<<endl;

随机打乱

uaing namespace rander;
vector<string> x{"e","k","r"};
randsort(x);
double a[4]={1.0,2.1,3.23,2.23};
randsort(a,0,3);

历史版本

1.0 for Windows

#include<bits/stdc++.h>
using namespace std;
class rander{
	private:
	vector<long long> list;
	int it,ssize;
	public:
	int reset(int size){if(size<=0)return -1;int seed=std::rand();mt19937 Rand(seed);ssize=size;it=0;list.clear();for(int i=1;i<=size;++i){list.push_back(Rand());}return seed;}
	int reset(){struct _timeb T;_ftime(&T);srand(T.millitm);return rander::reset(1000);}
	vector<long long> randlist(int size){int seed=ssize;reset(size);vector<long long> ret=list;reset(seed);return ret;}
	int rand(){if(ssize<=0)reset();int ret=list[it];it++;if(it>=ssize)reset(ssize);return ret;}
	int rand(int mod){int ret=rander::rand()%mod;return ret;}
	long long randabs(int mod){long long ret=abs(rander::rand());if(mod)ret%=mod;return ret;}
	long long rand(int l,int r){long long ret=rander::randabs(r-l+1)+l;return ret;}
	int randfrom(vector<int> &p){long long ret=rander::randabs(p.size());return p[ret];}
	string randfrom(vector<string> &p){long long ret=rander::randabs(p.size());return p[ret];}
};

1.5 for Windows

#include<bits/stdc++.h>
using namespace std;
namespace rander{
	vector<long long> list;
	int it,ssize;
	int reset(int size){if(size<=0)return -1;int seed=std::rand();mt19937 Rand(seed);ssize=size;it=0;list.clear();for(int i=1;i<=size;++i){list.push_back(Rand());}return seed;}
	int reset(){struct _timeb T;_ftime(&T);srand(T.millitm);return rander::reset(1000);}
	vector<long long> randlist(int size){int seed=ssize;reset(size);vector<long long> ret=list;reset(seed);return ret;}
	int rand(){if(ssize<=0)reset();int ret=list[it];it++;if(it>=ssize)reset(ssize);return ret;}
	int rand(int mod){int ret=rander::rand()%mod;return ret;}
	long long randabs(int mod){long long ret=abs(rander::rand());if(mod)ret%=mod;return ret;}
	long long rand(int l,int r){if(l<0)return rander::rand(0,r);long ret=rander::randabs(r-l+1)+l;return ret;}
	int from(vector<int> p){long long ret=rander::randabs(p.size());return p[ret];}
	string from(vector<string> p){long long ret=rander::randabs(p.size());return p[ret];}
};
posted @ 2024-05-22 11:49  HaneDaniko  阅读(23)  评论(0编辑  收藏  举报