random number

I'm trying to get 500 random numbers between 1-100 and my random number generator is giving bad numbers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  #include <iostream>
#include <cstdlib>
//Jacob Wilt
//Good Random Number Generator
using namespace std;
int total = 0;
const int size = 500;
void bubble(int []), print(const int []);
int main()
{
    int random[size];
    int RN, i;
    for (i = 0; i < size; i++)
    {
        RN = rand() % 100 + 1;
        random[RN]++;
        total = total + RN;
        cout << random[RN] << endl;
    }
    double average = total / size;
    cout << average;
    bubble(random);
    print(random);
    return 0;
}
void bubble(int array[])
{
    int swap, n;
    while (swap == 1)
    {
       swap = 0;
    for (n = 0; n < size; n++)
    {
        if (array[n] > array[n + 1])
        {
            int T;
            swap = 1;
            T = array[n];
            array[n] = array[n+1];
            array[n+1] = T;
            cout << array[n] << endl;
        }
    }
    }
}
void print(const int array[])
{
    //for (int a = 0; a < size; a++)
      //  cout << array[a] << "   \n";
}
Please describe your problem further. What does a bad number look like?

Some problems:
Line 20: average will be a double, but probably not the way you intend. You are performing an integer division and then putting it in a double, which means you won't have any fractional data. You must also cast either the divisor or dividend to a double on this line.

Line 28: Initialize swap to 1. Right now it is probably initialized to whatever happens to be hanging out in the stack space (which you'd have to be lucky for it to be 1), and your loop would rarely get entered.
Last edited on
You forgot to seed the random number generator.

You seed it by calling std::srand() once in your program preferably in main() and by feeding it a different value each time the program is run. To accomplish this, most people feed it time().

http://www.cplusplus.com/reference/cstdlib/srand/
http://www.cplusplus.com/reference/ctime/time/

1
2
3
4
5
6
7
8
9
10
11
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    srand(time(NULL));

    // ...
}
Also, IMO should initialize your random array and not depend on default values of zero for all elements.
I do want my average to be a double.
and my output looks like this
1
128
6
-1414878807
6
10813441
10817145
258
1
1606005204
-1078018627
closed account (D80DSL3A)
For lines 16 - 18 you have:
1
2
3
random[RN]++;
total = total + RN;
cout << random[RN] << endl;

Shouldn't that be:
1
2
3
random[i] = RN;
total = total + RN;
cout << random[i] << endl;

?
Last edited on
jwilt wrote:
I do want my average to be a double.

Yes, what I meant was that your current calculation won't give you the average you probably want. In addition to declaring average as double, you must also be sure that you perform real division. Since total and size are both integers, C++'s behavior is to perform integer division (12 / 5 = 2, but what you probably want is 2.4). To force it to perform real division, you can cast either the divisor or dividend to double:
double average = total / (double)size;
that outputs more accurate numbers but it still gives bad numbers at the start.
and if the random number is a "55" I want it to add a one to the 55th place in the array, because I have to calculate which number was produced the most and least. i'm not sure if it still adds one to the 55th spot in the array.

and okay, thank you.
closed account (D80DSL3A)
OH. I didn't realize that's what you're trying to do. Why is array size 500 if it is to contain the counts for only 100 numbers?
Also, when you sort those counts you will lose the association with the number each count represents. It is a confusing approach.
well I want it to loop 500 times and each spot in the array "should" have a value of 5 but since it is random some will have more or less and I have to figure that out.
but I just don't see why my random numbers are out of the scope of 1-100.
Topic archived. No new replies allowed.