#include <iostream> #include <stdlib.h> #include <ctime> int main() { // Create a number that's 0 or 1 srand (time(NULL)); int coin = rand() % 3; // If number is 0: Heads // If it is not 0: Tails if (coin == 0) { std::cout << "Heads\n"; } else { std::cout << "Tails\n"; } } Does this mean that there is a 1/2 chance? Could someone thoroughly explain this to me.----> int coin = rand() % 2; What if i were to change this to int coin = rand() % 3; what exactly does this translate to. Could someone explain this to me with other examples.
lhsbyrhs(left hand side and right hand side respectively).rand()returns an unsigned integer between 0 and RAND_MAX, which is at least 32,767. What's the maximum remainder you can have if you're dividing something by two? By three? You could have found this out yourself by searching for "what does rand() return" and "how does modulo work" en.cppreference.com/w/cpp/numeric/random/rand en.cppreference.com/w/cpp/language/operator_arithmetic