Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I switch between local and global settings for the initial state of a C++11 RNG?

Tags:

c++

c++11

prng

In the code given below, I would like to implement a flag (or something equally simple) that has the same effect as commenting out the local setting and using the global setting some times (yielding two different numbers in this example), and using the local setting at other times (yielding two identical numbers in this example).

I have tried the obvious "if" and "switch" structures without success.

#include <iostream>
#include <random> 

void print();

std::seed_seq seed{1, 2, 3, 4, 5}; 
std::mt19937 rng(seed); // *global* initial state
std::uniform_real_distribution<> rand01(0, 1); 

int main()
{
    print();
    print();

    return 0; 
}

void print()
{
    std::mt19937 rng(seed); // *local* initial state
    std::cout << rand01(rng) << std::endl;
}
like image 725
PeteL Avatar asked May 14 '15 07:05

PeteL


People also ask

What are the prerequisites for initial switch configuration?

Whether the configuration deployment of a switch is completed all at once or done in phases, the basic switch settings must first be configured. The initial management configuration includes setting IP addresses, passwords, and VLANs, which the prerequisites for future feature configuration. Prerequisites for Initial Switch configuration

Which global configuration command is used to set local user database?

Two global configuration commands are used to set local user database. Both commands do same job. Advantage of using secret option over password option is that in secret option password is stored in MD5 encryption format while in password option password is stored in plain text format.

How to maintain the mappings as global settings in BW?

To maintain the mappings as global settings in BW, logical mappings of the source system ID’s and its descriptions should be maintained. So that, its applicable in the entire BW system and populate the source system id/description. The below mentioned tables will contain the information regarding the currency/unit/fiscal variants.

What does <CR> mean in a switch command?

If prompt returns with <CR> only as an option, that means switch does not need any additional parameters to complete the command. You can execute the command in current condition. How to set name on switch Switch name can be set from global configuration mode.


1 Answers

Use a ternary and a reference:

std::mt19937& ref = flag ? rng : local;

Here, flag is the condition to test, rng is the "global" random object, and local is the more localised one.

Binding a reference to the result of a ternary is syntactically valid: you can't do it using an if statement, or similar as those are not expressions of the correct type.

From that point, use ref. This will be valid so long as rng and local remain in scope.

like image 156
Bathsheba Avatar answered Oct 26 '22 23:10

Bathsheba