1

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;

}
EMmett
  • 13
  • 3
  • 2
    `random = rand() % 2 & rand() % 2 & rand() % 2;` almost certainly doesn't do what you think it does. Try `rand() % 8` instead to get a number from 0 to 7. `cout << random` also won't print the number in binary. You might be better off with a 3 character string, you can set each position and easily print leading zeroes. It's also better for input if you expect it to be binary as well. – Retired Ninja Sep 03 '22 at 05:50
  • I knew something was completely messed up about using & to add up numbers! Anyways, thanks for answering my question. I got the thing working and I just wanted to thank you for being a big help in it. I don't know why I needed it but it looks hella cool! – EMmett Sep 03 '22 at 05:57
  • Extract a [mcve]. You would have found that this has nothing to do with RNGs but everything with how various operators work and maybe figured out the core issue yourself. As a new user here, please also take the [tour] and read [ask]. – Ulrich Eckhardt Sep 03 '22 at 06:28

1 Answers1

0

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;

}
abdo Salm
  • 1,678
  • 4
  • 12
  • 22