简体   繁体   中英

Sharing a random engine in multiple functions

so my question in a nutshell would be: How do I access a random engine initialized in main() from another function without passing the engine as an argument?

I read that is a good habit to initialize a random engine only once in a program. I would do it like this at the beginning of my main() function:

#include <iostream>
#include <random>
#include <ctime>

using std::cout;
using std::endl;
using std::default_random_engine; 
using std::uniform_int_distribution;

int main() {
  int seed = time(0);
  default_random_engine engine(seed);              
  uniform_int_distribution<int> dist(0,100); 
  cout << "Random num: " << engine(dist) << endl;
}

But how would I make it accessible to other functions? Eg, in a nested structure where function func_a calls func_b , which is supposed to do something with a random number generated by the engine. For example:

#include <iostream>
#include <random>
#include <ctime>

using std::cout;
using std::endl;
using std::default_random_engine; 
using std::uniform_int_distribution;

void func_b() {
  // do sth
  cout << "Print a random number: " << endl;
}

void func_a() {
  func_b();
}

int main() {
  int seed = time(0);
  default_random_engine engine(seed);              
  uniform_int_distribution<int> dist(0,100); 
  func_a();
}

EDIT: Problem Solved, thanks.

#include <iostream>
#include <random>
#include <ctime>


using std::cout;
using std::endl;
using std::default_random_engine; 
using std::uniform_int_distribution;

////////////////////////
// GLOBAL VARIABLES
////////////////////////

int seed = time(0);
default_random_engine engine(seed);

////////////////////////
// FUNCTION DEFINITIONS
////////////////////////

void func_b() {
  uniform_int_distribution<int> dist(0,100);
  cout << dist(engine) << endl;
}

void func_a() {
  func_b();
}

int main() {
  int seed = time(0);
  default_random_engine engine(seed);              
  uniform_int_distribution<int> dist(0,100); 
  func_a();
}

Thanks for all your help. I copy the solution to the answer section so it can be marked as accepted.

#include <iostream>
#include <random>
#include <ctime>


using std::cout;
using std::endl;
using std::default_random_engine; 
using std::uniform_int_distribution;

////////////////////////
// GLOBAL VARIABLES
////////////////////////

int seed = time(0);
default_random_engine engine(seed);

////////////////////////
// FUNCTION DEFINITIONS
////////////////////////

void func_b() {
  uniform_int_distribution<int> dist(0,100);
  cout << dist(engine) << endl;
}

void func_a() {
  func_b();
}

int main() {
  int seed = time(0);
  default_random_engine engine(seed);              
  uniform_int_distribution<int> dist(0,100); 
  func_a();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM