Noob question

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
Raimond
Posts: 16
Joined: January 30th, 2014, 1:54 pm

Noob question

Post by Raimond » October 26th, 2017, 10:57 am

Hello peeps, I'm at the snake tutorial right now, wish i had more time for tuts but for now i have one question.
i see chilli did something i can't fully understand and i wish you could help me.
when initializing the rng constructor
rng(std::random_device()()) he used 2 of "()", my question is why.
rng is from "std::mt19937 rng;"

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Noob question

Post by chili » October 26th, 2017, 4:00 pm

The first () constructs the random_device object, and the second () calls the function operator (the generator function) on that object.
Chili

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Noob question

Post by albinopapa » October 26th, 2017, 6:57 pm

If I do this:

Code: Select all

	std::mt19937 rng( std::random_device()( ) );
I get the error "Function returning function is not allowed".
However, if I do it in a class initializer list:

Code: Select all

class MyClass
{
public:
	MyClass()
		:
		rng( std::random_device()( ) )
	{}
private:
	std::mt19937 rng;
};
Everything builds fine.
If you create a temporary random number generator in a function, you must use braces to construct the random_device and parentheses to call the operator() ( generate function ).

Code: Select all

	std::mt19937 rng( std::random_device{}( ) );
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

cameron
Posts: 794
Joined: June 26th, 2012, 5:38 pm
Location: USA

Re: Noob question

Post by cameron » October 27th, 2017, 5:25 pm

Think that's because compiler thinks you're declaring a function.
Computer too slow? Consider running a VM on your toaster.

Post Reply