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