Thread: Passing random seed/device to a function

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    115

    Passing random seed/device to a function

    Hello! I am trying to pass a seed to several functions, each generating one random number from different distribution, e.g. This does not work, as each iteration (function call) produces the same number. But why? Thank you for your help!

    Code:
    #include <iostream>
    #include <random>
    
    using std::cout;
    using std::endl;
    
    typedef std::mt19937 rng_type;
    rng_type rng;
    
    auto D(rng_type r, int n)
    {
    	std::uniform_int_distribution<rng_type::result_type> dist(1, n);
    	rng_type::result_type random_number = dist(r);
    	return random_number;
    }
    
    auto Scatter(rng_type r)
    {
    	std::uniform_real_distribution<rng_type::result_type> dist(0, 1);
    	rng_type::result_type random_number = dist(r);
    	return random_number;
    }
    
    int main()
    {
      rng_type::result_type const seedval = std::random_device{}();
      rng.seed(seedval);
      
      for(int i = 0; i <= 10; ++i) cout << D(rng, 6) << ", " << Scatter(rng) << endl;
      
      return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > auto D(rng_type r, int n)
    > auto Scatter(rng_type r)
    Because you create brand new random number generators through their copy constructors each time you call the function.

    Make each of your r parameters a reference.

    It's like you were expecting this to produce different results.
    Code:
    int foo ( int a ) {
      a += 1;
      return a;
    }
    int main ( ) {
      int x = 42;
      for ( int i = 0 ; i < 10 ; i++ ) cout << foo(x) << endl;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    115
    Quote Originally Posted by Salem View Post
    > auto D(rng_type r, int n)
    > auto Scatter(rng_type r)
    Because you create brand new random number generators through their copy constructors each time you call the function.

    Make each of your r parameters a reference.
    OK thx that fixed it! The other problem was that the output of Scatter was recast to a fixed 0, so I got rid of the types in the functions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random number seed
    By titaniumnuke in forum C Programming
    Replies: 1
    Last Post: 10-15-2014, 11:23 PM
  2. Replies: 2
    Last Post: 11-22-2012, 05:39 PM
  3. Random Seed without Time function
    By Gabacus in forum C Programming
    Replies: 4
    Last Post: 03-14-2005, 02:13 AM
  4. Replies: 2
    Last Post: 11-07-2003, 12:21 AM
  5. write() function - no such device or address
    By threahdead in forum Linux Programming
    Replies: 3
    Last Post: 03-30-2003, 05:05 AM

Tags for this Thread