Ask Your Question
0

How to generate normally distributed random numbers ?

asked 2016-12-23 09:48:18 -0600

azdoud.y gravatar image

updated 2016-12-24 12:02:13 -0600

Dear community,

I'm trying to write a code aims to generate pseudorandom scalar drawn from the standard normal distribution. I have implemented these random functions normal_distribution but I still not getting my expectations.

I need to get this resultat, no matter how it's long :

    0.9089 
    0.4115
    0.0899 
    1.3467
    0.8019  
    -1.9613
    -0.1071  
    -2.8053
    -0.5398  
    -1.4261
    0.5250  
    -0.9969
    ...

Would be great to get your help. I appreciate any suggestions Thank you in advance

edit retag flag offensive close merge delete

Comments

I need them generated with a normal distribution with mean 1 and standard deviation 2 plz

azdoud.y gravatar imageazdoud.y ( 2016-12-23 14:47:35 -0600 )edit

I've wrote this what do you thing

    Size sM(1,1);
    Mat mean(sM,CV_64F), stddev(sM,CV_64F) ,dst(sM,CV_64F);
    mean.at<double>(0,0) = 1;
    stddev.at<double>(0,0) = 2;
    randn(dst, mean, stddev);
azdoud.y gravatar imageazdoud.y ( 2016-12-23 15:12:46 -0600 )edit

You can use this method too :

rng.fill(dst, RNG::NORMAL,1,2)

LBerger gravatar imageLBerger ( 2016-12-24 06:42:49 -0600 )edit

@azdoud.y - so, you have an example, above. what exactly, does not suit your needs, here ?

berak gravatar imageberak ( 2016-12-24 12:09:06 -0600 )edit

with all recpect to @LBerger and @Tetragramm, when I used rng.fill(dst, RNG::NORMAL,1,2) i don't get what I want I got this

[4.546826124191284]
[4.584695339202881]
[1.896207928657532]
[3.372463703155518]
[4.132120847702026]
[2.377070903778076]
[3.775224208831787]
[3.56936526298523]
[2.937457799911499]
[4.62383508682251]
[-1.535781621932983]
[3.329351902008057]
...

in rng.gaussian(sigma) I can't change the mean

azdoud.y gravatar imageazdoud.y ( 2016-12-24 13:00:44 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-12-23 10:53:34 -0600

Tetragramm gravatar image

This is really a C++ question, no OpenCV needed. I mean, there's the example on the page you linked to that does exactly what you want.

But OpenCV does have an RNG class so here's how to use that. The mean is 0, the sigma is a parameter to the gaussian function.

cv::RNG rng(cv::getCPUTickCount());
double random = rng.gaussian(sigma);
edit flag offensive delete link more

Comments

First thank you a lot, however, No Sir in the page that I linked doesn't make exactly what I want. in OpenCV there is a randn function that hope use it in my case with mean 1 and standard deviation 2

   //! fills array with normally-distributed random numbers with the specified mean and the standard deviation
   CV_EXPORTS_W void randn(InputOutputArray dst, InputArray mean, InputArray stddev);
azdoud.y gravatar imageazdoud.y ( 2016-12-23 11:13:33 -0600 )edit

To change the mean, just add the mean to the output values. And yes, the example in the page does what you need.

Tetragramm gravatar imageTetragramm ( 2016-12-25 11:55:41 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-12-23 09:48:18 -0600

Seen: 1,409 times

Last updated: Dec 24 '16