OK, I was working on a project where I needed to randomly generate tens of thousands of 1s and 0s. I started noticing it didn't seem very random.

To test it out I created an array of 458752 (896x512) ints. Then, I tested by seeing how many 1s were generated. I loop it 100 times to see if it is randomly generating different amounts of 1s. But, it only generates the same two amounts of 1s and 0s. Now these 2 amounts seem to be random after each program start(it is always around 229,000), but this is not random.

The problem is I can not randomly seem to generate a lot of 0s and 1s. Any ideas what I am doing wrong? I'm using MinGW(GCC) on Win right now, but will test on some other systems.

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

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

    int array_size = 458752;
    int array[array_size];

    int total1s =0;

    for(int q=0; q<100; q++) //testing 100 times
    {
        total1s=0; //setting total1s back to 0 each loop and will recount
        
        //Randomly entering 1s and 0s into the array
        for(int i=0; i<array_size; i++)
        {
            array[i] = (rand() % 2);
        }
        //Counting how many of the items are 1s
        for(int k=0; k<array_size; k++)
        {
            if(array[k] == 1 )
                total1s++;
        }
        cout << total1s<<endl;
    }
    return 0;
}

EXAMPLE Output I am getting:
229500
229252
229500
229252
229500
229252
229500
229252
.... etc

Recommended Answers

All 5 Replies

OK, I was working on a project where....

I made some simplified code that got rid of the huge array. And I am now generating 917504 1s and 0s. Now it always generates the exact same amount of 1s and 0s. 458752 1s and 458752 0s

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

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

    int amount_of_randoms = 917504;
    int total1s;

    for(int q=0; q<100; q++) //testing 100 times
    {
        total1s=0; //setting total1s back to 0 each loop and will recount

        //Randomly entering 1s and 0s into the array
        for(int i=0; i<amount_of_randoms; i++)
        {
            if( (rand() % 2) == 1)
                total1s++;
        }
        cout << total1s<<endl;
    }
    return 0;
}

EXAMPLE output:
458752
458752
458752
458752
458752
..... etc

rand()%2 is not guaranted to generate random number. Try something like rand()/((RAND_MAX+1)/2).

rand()%2 is not guaranted to generate random number.

rand is a pseudorandom number generator, it's never guaranteed to be random. Your suggestion is really based on the history of rand, where implementations had a tendency to have very predictable low order bits. The division solution fixed the problem by using high order bits instead.

Further reading here and here.

Thanks Zjarek that is working well for my purposes.

@Narue

Thanks. That is some interesting reading. I have a though about generating random numbers though.

I'm generating fairly large binary numbers. Something say between 0 and 2^16000. What I am currently doing is basically setting up a data structure and then generating 1s and 0s 16000 times. I end up with a large binary number sure, but how random?

Am I just fooling myself? I'm basically adding up a bunch of 50/50s. Over 16000 times I should tend to have around same numbers of 1s as 0s right? I guess what I am thinking is am I less likely to get say "4" doing it this way than if I was randomly choosing between 0 and 2^16000? 4 would basically be getting one 1 and 15999 0s.

If you need better pseudorandom generation algorithms, I'd look into c++0x / boost random library. I don't think rand() has sufficient properties (it's current implementations) to generate such huge number randomly.
@Narue You can see that this is true even for rather new compilers, I don't know why modulo method is so common. Possibility to use modulo arithmetic is not needed, it is sometimes useful addition.
I was saying about random numbers in meaning that you can treat them as random in most applications. You usually don't need true random number, for example mathematicians before computers used huge random number tables (available for sale).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.