예제 #1
0
FastDMSamplingBase::value_t FastDMSamplingInvNorm::rand(rngeng_t& rngeng)
{
    std::uniform_real_distribution<double> unif_dist;
    while (true) {
        const value_t t = randin(rngeng, invabsmu_, invmu2_);
        const value_t one2t = 0.5 / t;
        if (t < 2.5) {
            // short-time series
            if (acceptt(t, exp(-one2t) * unif_dist(rngeng), one2t)) return t;
        } else {
            // long-time series
            constexpr value_t Cl = -0.6773740579341821; // -log(pi/4)-log(2pi)/2;
            if (acceptt(t, exp(Cl - one2t - 3 / 2 * log(t)) * unif_dist(rngeng), 
                        PI * PI * t / 8)) return t;
        }
    }
}
예제 #2
0
void keygen (const char* keyfile)
{
	umask(0077); // make sure key file is protected
	std::ofstream	keyout(keyfile);
	if (!keyout) {
		perror(keyfile);
		std::exit(1);
	}
	std::ifstream	randin("/dev/random");
	if (!randin) {
		perror("/dev/random");
		std::exit(1);
	}
	char		buffer[AES_KEY_BITS/8 + HMAC_KEY_LEN];
	randin.read(buffer, sizeof(buffer));
	if (randin.gcount() != sizeof(buffer)) {
		std::clog << "Premature end of random data.\n";
		std::exit(1);
	}
	keyout.write(buffer, sizeof(buffer));
}