Register    Login    Forum    Search    FAQ

Board index » Everything




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Noob question
 Post Posted: October 26th, 2017, 10:57 am 
 

Joined: January 30th, 2014, 1:54 pm
Posts: 16
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;"


Top 
 Profile  
Reply with quote  
 Post subject: Re: Noob question
 Post Posted: October 26th, 2017, 4:00 pm 
Site Admin
User avatar

Joined: December 31st, 2011, 4:53 pm
Posts: 3538
Location: Japan
The first () constructs the random_device object, and the second () calls the function operator (the generator function) on that object.

_________________
Chili


Top 
 Profile  
Reply with quote  
 Post subject: Re: Noob question
 Post Posted: October 26th, 2017, 6:57 pm 
 

Joined: February 28th, 2013, 3:23 am
Posts: 2970
Location: Oklahoma, United States
If I do this:
Code:
   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:
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:
   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


Top 
 Profile  
Reply with quote  
 Post subject: Re: Noob question
 Post Posted: October 27th, 2017, 5:25 pm 
 

Joined: June 26th, 2012, 5:38 pm
Posts: 767
Location: USA
Think that's because compiler thinks you're declaring a function.

_________________
Computer too slow? Consider running a VM on your toaster.


Top 
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
 
Post new topic Reply to topic  [ 4 posts ] 

Board index » Everything


 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
cron