Ask Your Question
1

how to generate random point

asked 2016-12-20 18:03:11 -0600

azdoud.y gravatar image

updated 2016-12-21 00:55:22 -0600

Dear community,

I' have written this function to generate random value at each iteration in a for loop, however, it seems that it doesn't do the expected job

  Point initPt(int nx0, int ny0, int nx1, int ny1){
    RNG rng;
    Point pt;
    double D = rng.uniform((double) nx0, (double) nx1);
    pt.x = round(D);

    D = rng.uniform((double) ny0, (double) ny1);
    pt.y = round(D);

    return pt;
}

I wrote this demo loop to see the results

    for (int i=0; i<n ; i++){
        Point pt = initPt(nx0, ny0, nx1, ny1);
        nest.at<int>(i,1) = pt.x;
        nest.at<int>(i,2) = pt.y;
        cout << pt <<endl;
    }

I got, unfortunately, this

   [127, 110]
   [127, 110]
   [127, 110]
   [127, 110]
   [127, 110]
   [127, 110]
   [127, 110]
   ...

I am glad to any help

edit retag flag offensive close merge delete

Comments

nest.at<int>(i,1) <-- first index starts at 0

berak gravatar imageberak ( 2016-12-20 23:43:27 -0600 )edit

I have to change that, however it doesn't fix the mean problem

azdoud.y gravatar imageazdoud.y ( 2016-12-21 01:11:55 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
5

answered 2016-12-20 18:20:01 -0600

Tetragramm gravatar image

The RNG initializes to the same value every time you create it, so the output values are the same. I suggest seeding it with the current PC time (ex from getCPUTickCount().

edit flag offensive delete link more

Comments

how it's done ? with getCPUTickCount(). i've that same thing with srand(time(NULL)); rand() % nx1 + nx0;

azdoud.y gravatar imageazdoud.y ( 2016-12-21 07:37:50 -0600 )edit
2

.

RNG rng(getCPUTickCount());
Tetragramm gravatar imageTetragramm ( 2016-12-21 08:47:49 -0600 )edit

that's Great, you deserve a save life Badge thank you @Tetragramm

azdoud.y gravatar imageazdoud.y ( 2016-12-21 09:02:57 -0600 )edit

@azdoud.y it won't work with time(NULL), because that counts in seconds, and your code is way too fast, to get a different number then.

berak gravatar imageberak ( 2016-12-23 01:24:45 -0600 )edit

As you said it want too slow @berak, i'll get first solution thank you I'm glad to be a member here with kind person

azdoud.y gravatar imageazdoud.y ( 2016-12-23 01:31:58 -0600 )edit

Question Tools

Stats

Asked: 2016-12-20 18:03:11 -0600

Seen: 1,906 times

Last updated: Dec 21 '16