Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7922847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:06:44+00:00 2026-06-03T17:06:44+00:00

I am trying to generate random numbers from a normal distribution. When the code:

  • 0

I am trying to generate random numbers from a normal distribution. When the code:

normal(eng)

appears in main(), the program works fine. However, if it is called from another function, the next call from main returns the same value generated previously. Below is some code that illustrates this. The first few lines of output are:

-0.710449
-0.710449
0.311983
0.311983
1.72192
1.72192
0.303135
0.303135
0.456779
0.456779

Does anyone know why this occurs?

The compiler is gcc 4.4.1 on Windows.

#include <iostream>
#include <cmath>
#include <ctime>
#include <tr1/random>

typedef std::tr1::ranlux64_base_01 Engine;
//typedef std::tr1::mt19937 Engine;
typedef std::tr1::normal_distribution<double> Normal;

double random_normal(Engine eng, Normal dist) {
    return dist(eng);
}

int main ( int argc, char** argv ) {
    Engine eng;
    eng.seed((unsigned int) time(NULL));

    Normal normal(0,1);

    for (int i = 0; i < 100; i++)
    {
        std::cout << random_normal(eng, normal) << std::endl;
        std::cout << normal(eng) << std::endl;   
    }

    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T17:06:46+00:00Added an answer on June 3, 2026 at 5:06 pm

    This is happening because you’re passing the engine by value to random_normal. random_normal gets a copy of the engine and so the original engine does not have its state modified, and using that original engine directly will yield the same result as random_normal obtained.

    If you modify random_normal to take the engine by reference:

    double random_normal(Engine &eng, Normal Dist);
    

    then the original engine will be modified and you won’t get repeated values. All the standard distributions take their engines by reference. For example:

    template<class IntType = int>
    class uniform_int_distribution
    {
    ...
        // generating functions
        template<class URNG>
        result_type operator()(URNG& g);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to generate random numbers from a gaussian distribution. Python has the very
I'm trying to generate an array of random numbers from 0-n then shuffle (but
I'm trying to generate a bunch of random numbers using rand() % (range). Here's
Im trying to generate a color gradient using ColdFusion. My current code below works
What I am trying to do is to generate some random numbers (not necessarily
I am trying to generate random numbers (integers) in Go, to no avail. I
I am actually writing a program to generate some truely random numbers. So, i
I am trying to figure out how to generate random from using /dev/random and
I am trying to generate random floats using nothing but bytes I get from
I have some code to generate 4 unique random numbers between 0-9: - //Globals

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.