Home > Back-end >  The most simplest of C RNG questions that I'm embarrassed to come back and ask
The most simplest of C RNG questions that I'm embarrassed to come back and ask

Time:09-04

sorry for all the horrible questions

Ok so basically I have this short little C project thats supposed to spit out random 3-digit binary numbers and stop when it finds one that's matching to the binary string you, the user types in. I know something is wrong with the "&" part and really want to know what's wrong even though every time I go on here I get banished to hell for asking stupid questions.

Please help, and sorry for my stupid questions in the not so far back past.

#include <iostream>
#include <cstdlib>
#include <time.h>


using namespace std;
int main() {
srand((unsigned)time(0));

short random{ rand() % 2 & rand() % 2 & rand() % 2 };
short input{};
cin >> input;

    while (random != input) {
        cout << random << endl;

        random = rand() % 2 & rand() % 2 & rand() % 2;
        
    };
        

return 0;

}

CodePudding user response:

first of all , writing using namespace std; is a very bad practice : please refer to Why is "using namespace std;" considered bad practice?

second of all using bitwise AND will not give you what you want for the line of code you write which is random = rand() % 2 & rand() % 2 & rand() % 2;

then what do you think about the following test case : random = 0 & 1 & 1 ?

you want random to be 011 , right ?but actually according to your code , random will be 0 as anything you AND with zero will give you zero , so the random variable will be always either 0 or 1 , the only case when it will be 1 is when you : random = 1 & 1 & 1 , so this is wrong.

the actual way you may want to do as mentioned in the comments : instead of random = rand() % 2 & rand() % 2 & rand() % 2; , put random = rand() % 8 this will give you numbers from 0 to 7 only as 7 representation in binary is 111 which is the maximum value in 3 bit you want.

another way is instead of random = rand() % 2 & rand() % 2 & rand() % 2; , write random = rand(); you can modify the condition from while (random != input) { to while ((random & 0b111) != input) to test only the Least 3 significant bits in the random number , and here is the code using this method :

#include <iostream>
#include <cstdlib>
#include <time.h>

using namespace std;
int main() {

    srand((unsigned)time(0));

   short random = rand();
   short input{};
    cin >> input;

    while ((random & 0b111) != input) {
        cout << (random & 0b111) << endl;

       random = rand();

    };

    return 0;

}

also another way of doing it , if you want to randomize bits . you then want to randomize one bit by determining whether the number is even or odd then it's rand() % 2 but this is only for one bit but you have to determine its position so shift left this bit , so if it's the second bit then (rand() % 2) << 2 which if it was 1 then shifted to the left by 2 then it will become 100 in binary format and you have to OR not to AND in this case in order to manipulate the bit number 1 and 0, because if you AND then anything ANDED with zero is zero

and here is the full code :

    #include <iostream>
#include <cstdlib>
#include <time.h>

using namespace std;
int main() {
    srand((unsigned)time(0));

    short random = (rand() % 2 << 2) & (rand() % 2 << 1) & (rand() % 2 << 0);
    short input{};
    cin >> input;

    while (random != input) {
        cout << random << endl;

        random = ((rand() % 2) << 2) | ((rand() % 2) << 1) | ((rand() % 2) << 0);


    };

    return 0;

}
  • Related