Hi all,

I came across an old post in 2006 about RAND_MAX being 16bit only. I did a quick printf("%d\n", RAND_MAX) and I get 32767. I'm using the mingw that came with the latest CodeBlock for Windows as of writing, gcc version 4.4.1 TDM-2. What's the story there? It's giving me a lot of problems when using random_shuffle() for large data because it doesn't shuffle properly. I thought I was going nuts! I wrote a test program to verify the problem:

#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

void PrintHistogram(vector <int> &data)
{
    int bins = 10;
    vector <int> histogram(bins);
    float percentage = 0.1;

    // Sample a percentage and accumulate histogram
    for(int i=0; i < data.size()*percentage; i++) {
        int pos = bins*data[i]/data.size();
        histogram[pos]++;
    }

    for(unsigned int i=0; i < histogram.size(); i++) {
        printf("%d - %d\n", i, histogram[i]);
    }
}

int main(int argc, char **argv)
{
    vector <int> data(100000);

    for(unsigned int i=0; i < data.size(); i++) {
        data[i] = i;
    }

    random_shuffle(data.begin(), data.end());

    printf("\nRandom shuffle\n");
    PrintHistogram(data);

    return 0;
}

The program generates 100,000 data points and assigns values from 0 to 99,999. It then sub-samples 10% of this data, which we expect to have a uniform distribution if we plot a histogram of 10 bins.

On Windows 7 I get:

Random shuffle
0 - 360
1 - 348
2 - 459
3 - 462
4 - 598
5 - 740
6 - 1045
7 - 1417
8 - 1974
9 - 2598

Not a uniform distriubtion at all ! But on Linux I get:

Random shuffle
0 - 1033
1 - 1001
2 - 1009
3 - 1005
4 - 986
5 - 1030
6 - 942
7 - 965
8 - 1041
9 - 988

What we expect.

So the question is, how do I get more than 16bit from rand() ? This seems like a serious flaw.

Nghia