本文整理汇总了C++中std::default_random_engine类的典型用法代码示例。如果您正苦于以下问题:C++ default_random_engine类的具体用法?C++ default_random_engine怎么用?C++ default_random_engine使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了default_random_engine类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: RandomNum
/***********************************************************
* Generate a random number between 0 and 1.
****/
double RandomNum(bool reseed) {
static bool firstTime = true;
static std::default_random_engine gen;
static std::uniform_real_distribution<double> unif(0.0, 1.0);
if (firstTime || reseed) {
std::random_device rd;
gen.seed(rd());
firstTime = false;
}
return unif(gen);
}
示例2: genRandomVelocity
Vector2 Ball::genRandomVelocity() const
{
Vector2 vec;
static std::default_random_engine engine;
engine.seed(time(nullptr));
static std::bernoulli_distribution distribution;
vec.setX(distribution(engine) ? 1 : -1);
vec.setY(distribution(engine) ? 1 : -1);
return vec;
}
示例3: InitRandom
namespace math {
#ifdef HAVE_RANDOM
std::default_random_engine generator_;
#endif
void InitRandom() {
Clock clock;
TimeStamp now;
clock.CurrentTime(&now);
uint64_t seed = (static_cast<uint64_t>(now.MicroSeconds()) << 32) +
static_cast<uint64_t>(getpid());
#ifdef HAVE_RANDOM
generator_.seed(seed);
#elif defined(_WIN32)
srand(seed);
#else
srandom(seed);
#endif
}
int Random(int lower, int upper) {
#ifdef HAVE_RANDOM
std::uniform_int_distribution<int> distribution(lower, upper);
return distribution(generator_);
#elif defined(_WIN32)
return (lower +
(rand() % (upper - lower + 1))); // NOLINT(runtime/threadsafe_fn)
#else
return (lower + (random() % (upper - lower + 1)));
#endif
}
} // namespace math
示例4: gen
namespace Random {
std::default_random_engine gen(time(0));
sf::Color getRandomColor() {
return sf::Color(gen()%256, gen()%256, gen()%256);
}
unsigned long int getUnsignedRandom(unsigned long int min=0,
unsigned long int max=gen.max()) {
return min + (gen() % (max-min));
}
short int getRandomSign(bool zero=true) {
if(zero) return (-1 + getUnsignedRandom(0, 3));
else return (getUnsignedRandom(0, 11)%2==0)?-1:1;
}
long int getRandom(long int min=0, long int max=gen.max()/2-1) {
return getUnsignedRandom(0, math::abs(max-min))+min;
}
long int getBoundRandom(long int x1, long int x2, long int x3, long int x4) {
return getRandomSign(false)<0?getRandom(x1, x2):getRandom(x3, x4);
}
bool getRandomBool(void) {return getUnsignedRandom(0, 11)%2;}
}
示例5: GetRandomEggTextureFilepath
std::string EasterEggFactory::GetRandomEggTextureFilepath()
{
// MAKE SURE THAT THE RANDOM NUMBER GENERATOR HAS BEEN INITIALIZED.
static bool randomNumberGeneratorInitialized = false;
static std::default_random_engine randomNumberGenerator;
if (!randomNumberGeneratorInitialized)
{
// Initialize the random number generator with a seed based on the current time.
unsigned long seed = static_cast<unsigned long>(std::chrono::system_clock::now().time_since_epoch().count());
randomNumberGenerator.seed(seed);
randomNumberGeneratorInitialized = true;
}
// SELECT A RANDOM EGG TEXTURE.
// An random index is selected that is restricted to the valid range of indices
// into the egg texture array.
unsigned int randomEggTextureIndex = ( randomNumberGenerator() % EGG_TEXTURE_FILEPATHS.size() );
return EGG_TEXTURE_FILEPATHS[randomEggTextureIndex];
}
示例6:
Vertex::Vertex()
{
myWeight = 1;
parent = 0;
isDraggedAlong = false;
isAbsorbed = false;
noOfChild = 0;
ExtraWeight = 0;
myRealCommunity = -1;
gen.seed(QTime::currentTime().msec());
}
示例7: getOperation
TEST_P(DequeTest, TestCorrectOperations) {
std::uniform_int_distribution <int> getOperation(0, 5);
generator.seed(5);
std::deque <int> stldeq;
mydeque <int> mydeq;
for(size_t i = 0; i < GetParam(); ++i) {
Operations thisOper = static_cast<Operations> (getOperation(generator));
switch(thisOper) {
case PUSH_BACK: {
int tmpelem = dist(generator);
mydeq.push_back(tmpelem);
stldeq.push_back(tmpelem);
}
break;
case PUSH_FRONT: {
int tmpelem = dist(generator);
mydeq.push_back(tmpelem);
stldeq.push_back(tmpelem);
}
break;
case POP_BACK: {
if(stldeq.size()) {
mydeq.pop_back();
stldeq.pop_back();
}
}
break;
case POP_FRONT: {
if(stldeq.size()) {
mydeq.pop_front();
stldeq.pop_front();
}
}
break;
case SIZE_EQUAL: {
int size_stl = stldeq.end() - stldeq.begin();
int size_my = mydeq.end() - mydeq.begin();
ASSERT_EQ(size_stl, size_my);
}
break;
case EQUAL: {
ASSERT_TRUE(std::equal(mydeq.begin(), mydeq.end(), stldeq.begin()));
}
break;
default: ASSERT_TRUE(false);
break;
}
}
SUCCEED();
}
示例8: doInit
void doInit()
{
mutex.lock();
printf("Connecting with game server\n");
Network::connect(port.getName(), SERVER_NAME);
myLife=6;
randengine.seed(0);
mutex.unlock();
}
示例9: randomInteger
int randomInteger(int max, int min) {
if (max == min)
return max;
orAssertGreaterThan(max, min);
if (!engineIsSeeded) {
engine.seed(std::chrono::system_clock::now().time_since_epoch().count());
engineIsSeeded = true;
}
int range = max - min;
auto elem = distributions.find(range);
if (elem == distributions.end()) {
distributions[range] = std::uniform_int_distribution<int>(0, range);
return distributions[range](engine) + min;
} else {
return std::get<1>(*elem)(engine) + min;
}
}
示例10: main
int main(int argc, char** argv) {
// Google tools initialization
google::InitGoogleLogging(argv[0]);
google::SetUsageMessage(
"A Connect Four game based on Minimax with Alpha-Beta prunning.");
google::ParseCommandLineFlags(&argc, &argv, true);
// Random seed
PRNG.seed(FLAGS_seed);
// Show used options
LOG(INFO) << "-o " << FLAGS_o;
LOG(INFO) << "-rows " << FLAGS_rows;
LOG(INFO) << "-cols " << FLAGS_cols;
LOG(INFO) << "-seed " << FLAGS_seed;
LOG(INFO) << "-ai " << FLAGS_ai;
LOG(INFO) << "-max_depth " << FLAGS_max_depth;
LOG(INFO) << "-wh " << FLAGS_wh;
LOG(INFO) << "-random " << FLAGS_random;
// Play!
Game game;
game.Play();
return 0;
}
示例11: main
int main(int argc, char* argv[])
{
fstream myfile, gradfile;
gradfile.open("gradsRNN.txt", ios::trunc | ios::out);
double dt = 1.0;
double tau = 30.0;
int PHASE=LEARNING;
int RANDW = 0;
if (argc > 1)
for (int nn=1; nn < argc; nn++)
{
if (strcmp(argv[nn], "test") == 0) { PHASE = TESTING; cout << "Test mode. " << endl; }
if (strcmp(argv[nn], "RANDW") == 0) { RANDW = 1; } // Randomize weights. Only useful for 'test' mode.
if (strcmp(argv[nn], "METHOD") == 0) { METHOD = argv[nn+1]; }
if (strcmp(argv[nn], "MODULTYPE") == 0) { MODULTYPE = argv[nn+1]; }
if (strcmp(argv[nn], "SQUARING") == 0) { SQUARING = atoi(argv[nn+1]); }
if (strcmp(argv[nn], "DEBUG") == 0) { DEBUG = atoi(argv[nn+1]); }
if (strcmp(argv[nn], "G") == 0) { G = atof(argv[nn+1]); }
if (strcmp(argv[nn], "ETA") == 0) { ETA = atof(argv[nn+1]); }
if (strcmp(argv[nn], "TAU") == 0) { tau = atof(argv[nn+1]); }
//if (strcmp(argv[nn], "INPUTMULT") == 0) { INPUTMULT = atof(argv[nn+1]); }
if (strcmp(argv[nn], "ALPHAMODUL") == 0) { ALPHAMODUL = atof(argv[nn+1]); }
if (strcmp(argv[nn], "PROBAMODUL") == 0) { PROBAMODUL = atof(argv[nn+1]); }
if (strcmp(argv[nn], "ALPHATRACE") == 0) { ALPHATRACE = atof(argv[nn+1]); }
if (strcmp(argv[nn], "ALPHATRACEEXC") == 0) { ALPHATRACEEXC = atof(argv[nn+1]); }
if (strcmp(argv[nn], "RNGSEED") == 0) { RNGSEED = atof(argv[nn+1]); }
if (strcmp(argv[nn], "MAXDW") == 0) { MAXDW = atof(argv[nn+1]); }
}
string SUFFIX = "_G" + to_string(G) + "_MAXDW" + to_string(MAXDW) + "_ETA" + to_string(ETA) + "_ALPHAMODUL" + to_string(ALPHAMODUL) + "_PROBAMODUL" + to_string(PROBAMODUL) + "_SQUARING" +to_string(SQUARING) + "_MODULTYPE-" + MODULTYPE +
"_ALPHATRACE" + to_string(ALPHATRACE) + "_METHOD-" + METHOD + "_ATRACEEXC" + to_string(ALPHATRACEEXC) + "_TAU" + to_string(tau) +
//"_INPUTMULT" +to_string(INPUTMULT) +
"_RNGSEED" +
to_string(RNGSEED);
cout << SUFFIX << endl;
myrng.seed(RNGSEED);
srand(RNGSEED);
int trialtype;
int NBTRIALS = 507;
int TRIALTIME = 300;
int TIMEINPUT = 100;
int TIMEMODUL = 210;
int TIMERESP = 200;
VectorXi modulmarker(NBNEUR); modulmarker.setZero();
if (PHASE == TESTING)
{
NBTRIALS = 20*NBPATTERNS;
//NBTRIALS = 40*NBPATTERNS;
//TRIALTIME = 1500;
}
MatrixXd dJ(NBNEUR, NBNEUR); dJ.setZero();
MatrixXd win(NBNEUR, NBIN); win.setRandom(); //win.row(0).setZero(); // Input weights are uniformly chosen between -1 and 1, except possibly for output cell (not even necessary). No plasticity for input weights.
win *= .2;
MatrixXd J(NBNEUR, NBNEUR);
cout << Uniform(myrng) << endl;
randJ(J); // Randomize recurrent weight matrix, according to the Sompolinsky method (Gaussian(0,1), divided by sqrt(ProbaConn*N) and multiplied by G - see definition of randJ() below).
// If in the TESTING mode, read the weights from a previously saved file:
if (PHASE == TESTING){
if (RANDW == 0){
//readWeights(J, "J.dat");
//readWeights(win, "win.dat");
readWeights(J, "J" + SUFFIX + ".dat");
readWeights(win, "win" + SUFFIX + ".dat"); // win doesn't change over time.
}
else cout << "Using random weights." << endl;
}
//cout << J(0,0) << " " << win(1,1) << endl;
VectorXd meanabserrs(NBTRIALS); meanabserrs.setZero();
VectorXd lateral_input;
VectorXd total_exc(NBNEUR), delta_total_exc(NBNEUR), delta_total_exc_sq(NBNEUR), total_exc_prev(NBNEUR);
VectorXd delta_r(NBNEUR), delta_r_sq(NBNEUR), r_trace(NBNEUR), r_trace2(NBNEUR);
r_trace.fill(0); r_trace2.fill(0);
VectorXd delta_x(NBNEUR), delta_x_sq(NBNEUR), x_trace(NBNEUR), x_trace2(NBNEUR), delta_x_cu(NBNEUR);
//.........这里部分代码省略.........
示例12:
RandomModule() {
random_engine.seed(std::random_device{}());
}
示例13: getRandom
long int getRandom(long int min=0, long int max=gen.max()/2-1) {
return getUnsignedRandom(0, math::abs(max-min))+min;
}
示例14: getUnsignedRandom
unsigned long int getUnsignedRandom(unsigned long int min=0,
unsigned long int max=gen.max()) {
return min + (gen() % (max-min));
}
示例15: set_seed
void set_seed(int s) {
rng.seed(s);
}