-4

I am trying to generate three random number inside a for loop:

for (i=0; i<100; i++)
{
  item1 = rand()/(double)RAND_MAX*100;
  item2 = rand()/(double)RAND_MAX*200;
  item3 = rand()/(double)RAND_MAX*300;
}

So my question is:

How to get equally spaced random number for each item1, 2 and 3

like item1 = 10,20,30,40,50 item2 = 40, 60, 80, item3 = 25,35,45, etc.

3
  • 1
    If they are equally spaced, then they are not random. Perhaps you can make it clear what you mean. Feb 5, 2014 at 21:25
  • user sehe got it right.
    – snoze
    Feb 5, 2014 at 23:02
  • Do you appreciate that your question was poor? You should edit it to make it clear what you mean. Feb 5, 2014 at 23:06

3 Answers 3

0

You just want to round random numbers, or scale/project them after the generating.

This is... multiplication. Sample: (Live On Coliru)

#include <random>
#include <iostream>

int main()
{
    std::mt19937 engine(time(NULL));
    std::uniform_int_distribution<int> dist(0,10); // arbitrary range chosen

    for (int i=30; i>0; --i)
    {
        int random = dist(engine);
        std::cout << (random*10) << "\t" << (random*20) << "\t" << (random*10 + 15) << "\n";
    }
}

Output:

90  180 105
30  60  45
90  180 105
50  100 65
70  140 85
10  20  25
40  80  55
30  60  45
80  160 95
60  120 75
0   0   15
40  80  55
20  40  35
20  40  35
90  180 105
50  100 65
80  160 95
0   0   15
0   0   15
0   0   15
40  80  55
50  100 65
10  20  25
60  120 75
100 200 115
80  160 95
70  140 85
90  180 105
30  60  45
60  120 75
3
  • Thanks for your reply. I can not use the library you are using. My gnu g++ doesn't support it. However, in my code I am unable to get exact 10,20 etc number like you got. Do you think some change in my code so that I can use gnu g++ compiler. Thanks a lot for your time.
    – snoze
    Feb 5, 2014 at 22:40
  • Got it. with g++ -std=c++11 option your code is working. You saved my day. Thanks a lot.
    – snoze
    Feb 5, 2014 at 22:48
  • Please be aware that I limited the range of distinct outputs to 11 values (0..10) for the sake of the demo. This might not suit you. Change the bounds parameters on dist to adjust :)
    – sehe
    Feb 5, 2014 at 23:06
0

The numbers you want is not at all random infact this is sequence

You can do something like

int sequence =5;
int initial=10;

For (i=0; i<100; i++)
{
item1 = initial;
initial=initial+sequence;
item2 = initial;
initial=initial+sequence;
item3 = initial;
initial=initial+sequence;
}

It will generate 10, 15, 20, ...

-1

Maybe you are trying to get initial random number of a random equally spaced sequence, like this:

for (i=0; i<100; i++)
{
    if(i == 0)
    {
        item1 = rand()/(double)RAND_MAX*100;
        item2 = rand()/(double)RAND_MAX*200;
        item3 = rand()/(double)RAND_MAX*300;

        increment1 = rand()/(double)RAND_MAX*100;
        increment2 = rand()/(double)RAND_MAX*200;
        increment3 = rand()/(double)RAND_MAX*300;
    }
    else
    {
        item1 += increment1;
        item2 += increment2;
        item3 += increment3;
    }

    //do stuff
}
2
  • I want to keep the number between 1 to 100.
    – snoze
    Feb 5, 2014 at 22:45
  • Which number, item or increment? Do you also need to keep increment fixed?
    – Felipe
    Feb 5, 2014 at 23:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.