直接上代码
#include<bits/stdc++.h>
#include<windows.h>
#include<wincrypt.h>
using namespace std;
class WinRandom
{
HCRYPTPROV handle;
public:
WinRandom()
{
handle=NULL;
CryptAcquireContext((HCRYPTPROV*)&handle,NULL,NULL,PROV_RSA_FULL,0);
}
~WinRandom()
{
if(handle!=NULL) CryptReleaseContext(handle,0);
}
bool randBuf(void *dest,int len)
{
if (handle==NULL) return false;
return CryptGenRandom(handle,len,(BYTE*)dest);
}
#define _(func,typ)\
typ func()\
{\
typ ret=0;\
assert(randBuf((void*)&ret,sizeof(ret)));\
return ret;\
}
_(randInt,int)
_(randLong,long long)
_(randUnsigned,unsigned)
_(randUnsignedLong,unsigned long long)
_(randShort,short)
_(randUnsignedShort,unsigned short)
_(randChar,char)
_(randUnsignedChar,unsigned char)
_(randSignedChar,signed char)
};
int main()
{
WinRandom R;
cout<<R.randInt()<<endl; // -2147483648 ~ 2147483647
cout<<R.randUnsigned()<<endl; // 0 ~ 4294967295
cout<<R.randLong()<<endl; // -2^63 ~ 2^63-1
cout<<R.randUnsignedLong()<<endl; // 0 ~ 2^64-1
return 0;
}