samedi 16 avril 2016

Generating Random arrays giving me same results in C++

I'm trying to generate 8 sets of random characters that are either 0 or 1 in my program but for whatever reason it doesn't actually generate fully random arrays. to generate the random arrays I'm using this code:

while (true) {
    srand(static_cast<unsigned int>(time(0)));
    OnBit = 0;
    while (OnBit < 8) {
        Rand = rand() % 10 + 1;

        if (Rand > 5) { a[OnBit] = '1'; }
        else { a[OnBit] = '0'; }
        OnBit++;
    }

There are 8 while loops total in my program, one for each array. All of the loops are exactly like this one but change a different array's characters. Later in the program I use cout to output to the console the contents of each array. The problem I'm having is that the while most of the arrays generate randomly, the 7th array is always nothing. When it outputs all the arrays on separate lines the 7th lines is always blank, but all the others are randomly generated sets of 0s and 1s. Why cant the 7th array seem to actually be created?




Aucun commentaire:

Enregistrer un commentaire