在程序设计时,有时候我们需要生成随机数和数字摘要。在Poco库中,也提供了上述功能,下面我们一一叙述:
2. void seed()
使用任意值(从RandomInputStream类中获取)生成随机数。
3. 默认的构造时,Poco::Random类采用当前的时间和日期生成随机数。如果想要更好的随机效果,需要显式的调用seed()方法
4. UInt32 next()
返回0 ~ 2^31之间的随机整数
5. UInt32 next(UInt32 n)
返回0 ~ n之间的随机整数
6. char nextChar()
返回随机Char值
7. bool nextBool()
返回随机bool值
8. float nextFloat()
返回随机float值,范围0 ~ 1
9. double nextDouble()
返回随机double值,范围0 ~ 1
下面是关于Random的一个例子:
#include "Poco/Random.h"
#include "Poco/RandomStream.h"
#include
using Poco::Random;
using Poco::RandomInputStream;
int main(int argc, char** argv)
{
Random rnd;
rnd.seed();
std::cout << "Random integer: " << rnd.next() << std::endl;
std::cout << "Random digit: " << rnd.next(10) << std::endl;
std::cout << "Random char: " << rnd.nextChar() << std::endl;
std::cout << "Random bool: " << rnd.nextBool() << std::endl;
std::cout << "Random double: " << rnd.nextDouble() << std::endl;
RandomInputStream ri;
std::string rs;
ri >> rs;
return 0;
}
#include "Poco/HMACEngine.h"
#include "Poco/SHA1Engine.h"
using Poco::DigestEngine;
using Poco::HMACEngine;
using Poco::SHA1Engine;
int main(int argc, char** argv)
{
std::string message1("This is a top-secret message.");
std::string message2("Don't tell anyone!");
std::string passphrase("s3cr3t"); // HMAC needs a passphrase
HMACEngine hmac(passphrase); // we'll compute a HMAC-SHA1
hmac.update(message1);
hmac.update(message2);
const DigestEngine::Digest& digest = hmac.digest();
// finish HMAC computation and obtain digest
std::string digestString(DigestEngine::digestToHex(digest));
// convert to a string of hexadecimal numbers
return 0;
}
#include "Poco/DigestStream.h"
#include "Poco/MD5Engine.h"
using Poco::DigestOutputStream;
using Poco::DigestEngine;
using Poco::MD5Engine;
int main(int argc, char** argv)
{
MD5Engine md5;
DigestOutputStream ostr(md5);
ostr << "This is some text";
ostr.flush(); // Ensure everything gets passed to the digest engine
const DigestEngine::Digest& digest = md5.digest(); // obtain result
std::string result = DigestEngine::digestToHex(digest);
return 0;
}