3 Replies - 272 Views - Last Post: 12 February 2019 - 04:52 AM Rate Topic: -----

#1 Nwb   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 98
  • Joined: 11-November 18

Static vs Global (with <random>)

Posted 11 February 2019 - 04:30 AM

The identifiers used in this post are from <random>, they are used to generate random numbers. Yes very complicated.

I'm trying to make a function that generates a random number with given interval using <random> (the C++ way!). I want to avoid passing by arguments because the code becomes very messy and there isn't performance gain.

I'm currently using this:

namespace random_gen {
	std::random_device random_number_generator_seed;
	std::mt19937 random_number_generator(random_number_generator_seed());
	std::uniform_int_distribution<unsigned int> random_interval;
} // Reponsible for initializing random number generator

unsigned short int get_random_number(unsigned short int lower_limit, unsigned short int higher_limit) {
	random_gen::random_interval.param(std::uniform_int_distribution<unsigned int>::param_type(lower_limit, higher_limit));
	return random_gen::random_interval(random_gen::random_number_generator_seed);
}

(Globally declaring the things and using one function)

But would this be better?
unsigned short int get_random_number(unsigned short int lower_limit, unsigned short int higher_limit) {
	static std::random_device random_number_generator_seed;
	static std::mt19937 random_number_generator(random_number_generator_seed());
	static std::uniform_int_distribution<unsigned int> random_interval;
	random_gen::random_interval.param(std::uniform_int_distribution<unsigned int>::param_type(lower_limit, higher_limit));
	return random_gen::random_interval(random_gen::random_number_generator_seed);
}

(using one function, declaring the objects as static)

My doubt is: would the static objects call their constructors every time the function is called or would they not? I only know that static prevent reallocation of memory but I don't know these specifics.

Given the answer to the previous question, what would you prefer (the first code snippet or the second)? Or would you prefer passing arguments to function?

(sorry for the long names if it's hard to read)

This post has been edited by Nwb: 11 February 2019 - 04:39 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Static vs Global (with <random>)

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6763
  • View blog
  • Posts: 23,064
  • Joined: 05-May 12

Re: Static vs Global (with <random>)

Posted 11 February 2019 - 06:56 PM

You could create a small test program with our own classes and set breakpoints in the constructors and destructors to answer your question regarding how objects are created when they are declared as static.
Was This Post Helpful? 0
  • +
  • -

#3 Nwb   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 98
  • Joined: 11-November 18

Re: Static vs Global (with <random>)

Posted 11 February 2019 - 10:10 PM

Okay the constructor isn't called even if there is a new value.
#include <iostream> 
using namespace std;

class Foo {
public:
	Foo(int a) {
		cout << "CONSTRUCTOR CALLED";
		var = a;
	}
	~Foo() {
		cout << "DESTRUCTOR CALLED";
	}
	int var;
};

void bar(int a) {
	static Foo obj(a);
	cout << obj.var;
}

int main() {
	bar(5);
	bar(3);
	cin.get();
}


So the first and second snippet are pretty much the same. Which would you prefer?
btw random::gen shouldn't be in my second snippet.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6763
  • View blog
  • Posts: 23,064
  • Joined: 05-May 12

Re: Static vs Global (with <random>)

Posted 12 February 2019 - 04:52 AM

Good job experimenting.

Personally, I feel that the more encapsulation the better, so use the one with the private static variables rather than the globals.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1