rand() prefers higher numbers?

So for my dad I had to make this little program test the the reliability of the function rand(). I seed it, as usual, with time. But I've always thought it would give more lower numbers, but here, it seems to give a lot more high numbers...


This is the source:

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
52
53
54
55
56
57
#include <iostream>
#include <cstdlib>
#include <vector>
#include <ctime>

using namespace std;

int main()
{
    const int MAX = 50;

    vector<int> numGen;
    for(int s = 0; s < 10; ++s)
    {
        numGen.push_back(0);
    }

    srand ( time(NULL) );
    cout << "Press enter to start the test.";
    cin.get();
    int number[MAX];

    for(int i = 0; i < MAX; ++i)
    {
        number[i] = rand() % 10;
    }

    for(int k = 0; k < MAX; ++k)
    {
        switch(number[k]) {


        case 0 : numGen[0] += 1;
        case 1 : numGen[1] += 1;
        case 2 : numGen[2] += 1;
        case 3 : numGen[3] += 1;
        case 4 : numGen[4] += 1;
        case 5 : numGen[5] += 1;
        case 6 : numGen[6] += 1;
        case 7 : numGen[7] += 1;
        case 8 : numGen[8] += 1;
        case 9 : numGen[9] += 1;
        }
    }

    for(int j = 0; j < 10; ++j)
    {
        cout<<j<<" : ";
        for(int l = 0; l < numGen[j]; ++l)
        {
            cout<<"|";
        }
        cout<<"\n";
    }

    return 0;
}
That's because all cases after the matching one are executed as well.
Replace the entire switch by numGen[number[k]]++; and it will work as expected.

The code can be shrinked down even more:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cstdlib>
#include <vector>
#include <ctime>
#include <string>
using namespace std;

int main()
{
   const int numberLimit=10;
   const int numberCount=50;

   srand(time(NULL));
   cout << "Press enter to start the test.";
   cin.get();

   vector<int> numGen(numberLimit);
   for (int i=0;i<numberCount;i++)numGen[rand()%numberLimit]++;
   for (int i=0;i<numberLimit;i++)cout << i << " : " << string(numGen[i],'|') << endl;
}
What do you mean with "replace the entire switch? Also delete the cases?

EDIT: Ok, so i put break; after all the cases. But I don't see why it made a difference (it did)?
Last edited on
xander333 wrote:
What do you mean with "replace the entire switch? Also delete the cases?

Yes, duh.

xander333 wrote:
But I don't see why it made a difference (it did)?

See
That's because all cases after the matching one are executed as well.
Yes, but those cases should be false, so they aren't executed, right?
They ARE executed, that's what I'm saying.
For example, if case 6 applies, cases 7, 8 and 9 will also be executed.
Oh, ok!
Thanks a lot :)

thread solved!
BTW, the libc's randomizer is pretty weak (that is, predicatable). You might want to google around some more advanced randomizers if you are planning to write a nice game or something.

Good luck!
Topic archived. No new replies allowed.