I don't understand why I am getting the largest value repeated

Apr 14, 2013 at 11:53pm
Hi. I am trying to get an array of random numbers from 1-100 divisible by 11 with a for loop, so I did this:

void Array()
{
int k,m;
//m=rand();
for (k = 0; k < 20; k++)
{

m = rand();
for (m = 0; m < 100; m++)
{


SetCursorPosition(k+10,24);
SetTextColor(5);
if (m % 11==0)
{
cout << "The random value of the array = " << m;
}

}
}

}

I got 20 '99's instead. Can you tell me why and maybe point me in the right direction please? Thank you
Apr 15, 2013 at 12:09am
Did you seed the random number generator?

1
2
3
4
5
6
srand(time(0));

for (unsigned i = 0; i < 10; ++i) {
 random_number = 11 * (rand() % 10);
 cout << "randum number " << i << " = " << random_number << endl;
}

Last edited on Apr 15, 2013 at 12:09am
Apr 15, 2013 at 12:35am
I don't think I've seen this. Would it replace my second for loop?
Apr 15, 2013 at 1:25am
I've shown you how to generate numbers that divide equally by 11 without having to smash the random number generator over n over.
Topic archived. No new replies allowed.