Fail to generate gaussian distribution with seed

user3677103

I am trying to make a Gaussian distribution in visual studio C++ 2010. I want to have different results each time this is run. But when I run this code three times the result is same:

#include <iostream>
#include <random>
int roundnew(double d)
{
  return floor(d + 0.5);
}
int main()
{
    std::default_random_engine generator;
    std::normal_distribution<double> distribution(10,1);

    for (int n = 0; n < 12; ++n) {
       printf("%d\n",roundnew(distribution(generator)));
    }

return 0;
}

The result is

10
9
11
9
10
11
10
9
10
10
12
10

What is problem in my code? It needs seed value in my code, right? You can see the result at run code

shuttle87

You need to seed your random number generator.

Do something like this:

std::random_device rd;
std::default_random_engine generator;
generator.seed( rd() ); //Now this is seeded differently each time.
std::normal_distribution<double> distribution(10,1);

for (int n = 0; n < 12; ++n) {
{
    printf("%d\n",roundnew(distribution(generator)));
}

std::random_device is supposed to generate non-deterministic numbers so it should be good for your seeding purposes. Each program run should create a different seed to your RNG. See: http://en.cppreference.com/w/cpp/numeric/random/random_device (As R. Martinho Fernandes points out some implementations are lacking in this regard so if you are doing something important do check the implementation details.)

For more details about the c++ random number generation see this: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3551.pdf

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to generate a multiplicate 2D Gaussian Image distribution in MATLAB

From Dev

Fitting an image to Gaussian distribution

From Dev

Multiple Gaussian distribution

From Dev

Dealing with normal (Gaussian) distribution

From Dev

How generate pseudo-random numbers in uniform and gaussian distribution without float/double numbers?

From Dev

Strange behaviour with Gaussian random distribution

From Dev

Gaussian distribution for censored regression with cenreg

From Dev

Inverse of the cumulative gaussian distribution in R

From Dev

Multivariate Gaussian distribution formula implementation

From Dev

Variance using Gaussian distribution Python

From Dev

square of a number using gaussian distribution

From Dev

Generating 3D Gaussian distribution in Python

From Java

Gaussian distribution function different answer for program and math

From Dev

How to fit a double Gaussian distribution in Python?

From Dev

How to plot a Gaussian Distribution on y-axis?

From Dev

Apply a gaussian distribution in a specific part of an image

From Dev

Python - Integrating entire list with the Gaussian distribution

From Dev

Fitting bimodal gaussian distribution with some parameters fixed

From Dev

Create a matrix ( mx n) with gaussian random distribution

From Dev

How to extract values fitted to a gaussian distribution in R?

From Dev

redistribute data in Gaussian distribution with pd.DataFrame

From Dev

Python - Generate distribution

From Dev

Siesta generate screenshot on fail

From Dev

efficiently generate "shifted" gaussian kernel in python

From Dev

Generate array with 3 gaussian distributions MATLAB

From Dev

Generate a Gaussian kernel given mean and standard deviation

From Dev

rand() doesn't follow Gaussian Distribution & Central Limit Theorem

From Dev

How to make random loss bits follow Gaussian distribution

From Dev

JavaScript Math.random Normal distribution (Gaussian bell curve)?

Related Related

  1. 1

    How to generate a multiplicate 2D Gaussian Image distribution in MATLAB

  2. 2

    Fitting an image to Gaussian distribution

  3. 3

    Multiple Gaussian distribution

  4. 4

    Dealing with normal (Gaussian) distribution

  5. 5

    How generate pseudo-random numbers in uniform and gaussian distribution without float/double numbers?

  6. 6

    Strange behaviour with Gaussian random distribution

  7. 7

    Gaussian distribution for censored regression with cenreg

  8. 8

    Inverse of the cumulative gaussian distribution in R

  9. 9

    Multivariate Gaussian distribution formula implementation

  10. 10

    Variance using Gaussian distribution Python

  11. 11

    square of a number using gaussian distribution

  12. 12

    Generating 3D Gaussian distribution in Python

  13. 13

    Gaussian distribution function different answer for program and math

  14. 14

    How to fit a double Gaussian distribution in Python?

  15. 15

    How to plot a Gaussian Distribution on y-axis?

  16. 16

    Apply a gaussian distribution in a specific part of an image

  17. 17

    Python - Integrating entire list with the Gaussian distribution

  18. 18

    Fitting bimodal gaussian distribution with some parameters fixed

  19. 19

    Create a matrix ( mx n) with gaussian random distribution

  20. 20

    How to extract values fitted to a gaussian distribution in R?

  21. 21

    redistribute data in Gaussian distribution with pd.DataFrame

  22. 22

    Python - Generate distribution

  23. 23

    Siesta generate screenshot on fail

  24. 24

    efficiently generate "shifted" gaussian kernel in python

  25. 25

    Generate array with 3 gaussian distributions MATLAB

  26. 26

    Generate a Gaussian kernel given mean and standard deviation

  27. 27

    rand() doesn't follow Gaussian Distribution & Central Limit Theorem

  28. 28

    How to make random loss bits follow Gaussian distribution

  29. 29

    JavaScript Math.random Normal distribution (Gaussian bell curve)?

HotTag

Archive