vendredi 15 mai 2020

How does modulo work when simulating a coin toss in c++?

#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.




Aucun commentaire:

Enregistrer un commentaire