Fooji
Fooji

Reputation: 71

Need help understanding " rand() % 67 < 10"

I am trying to practice my C++ in order to grasp a better understanding in class, as I am a beginner and a little behind, and I came across a zombie game that someone posted online.

Anyways, I understand most of the code, however I do not understand "if(rand() & 67 < 10)".

I can interpret "rand() % 10 + 1", in which the code will generate a number, or in this case zombies, between 1 and 10.

Thinking at first that the zombies would cap at 67, given the player had chosen a high number, but I don't believe that is its function, but again, I'm not entirely sure...

Here is an example of the code I am looking at:

enter image description here

I'm sure its something simple, however I am still confused on its purpose. Even after trying to learn its function my running the game

Here is the whole code just in case:

#include <iostream>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>

using namespace std;

int createZombie() { // createZombie function OPEN

    if(rand() % 67 < 10)

            return 11;

    else
            return rand() % 10 + 1;

} // createZombie fuction CLOSED


// #################### MAIN FUNCTION ############################


int main() { // main function OPEN


    srand(time(NULL));

    char enter;

    // game stats

    int playerAlive = true;
    int playerSkill = 9;
    int playerScore = 1;
    string playerName = "";
    int zombieCount = 0;
    int zombiesKILLed = 0;

    // title


    cout << "Welcome to Zombie War." << endl << "Press [ENTER] to start ";
    cin.get();


    // player name

    cout << "Please enter your name: ";
    cin >> playerName;

    // ask how many zombies

    cout << "How many zombies do you wish to fight? ";
    cin >> zombieCount;


cout <<"Get ready to fight for you life, " << playerName << "!" << endl;

    // main game loop


    while(playerAlive && zombiesKILLed < zombieCount) { // while loop OPEN


            // create a random zombie

            int zombieSkill = createZombie();

            // battle sequence 

            if(zombieSkill > 10) { // if statment OPEN


                    cout << endl << "Here comes a huge zombie! " << endl;

            } // if statement CLOSED

            else { // else statement OPEN

                    cout << endl << "Here comes Zombie " << zombiesKILLed + 1 << endl;

            } // else statement CLOSED

            cout << "Fighting... " << endl;
            sleep(2);

            // zombies killed the player

            if(playerSkill < zombieSkill) { // if statement OPEN


                    playerAlive = false;

                    cout << "You have died." << endl;

            } // if statement CLOSED

            else { // else statment OPEN

                    // PLAYER KILLED THE ZOMBIE

                    if(playerSkill - zombieSkill > 7) { // if statement OPEN

                            cout << "You wasted the Zombie! " << endl;

                            playerScore = playerScore * 2;

 } // if statment CLOSED

                    else if (playerSkill - zombieSkill > 5) { // else if statement OPEN

                            cout << "You decapitated the Zombie!" << endl;

                            playerScore = playerScore * 2;

                    } // else if statement CLOSED

                    else if (playerSkill - zombieSkill > 0) { // else 
if statement OPEN

                            cout << "You Killed the Zombie!" << endl;

                            playerScore = playerScore * 2;

                    } // else if statment CLOSED

                    else { // else statment OPEN


                            cout << "You killed the zombie, but suffered injuries." << endl;


                    } // else statment CLOSED

                    zombiesKILLed++;


            } // else statment CLOSE


            cout << endl;
            sleep(1);

    } // while loop CLOSED



    if(zombiesKILLed == zombieCount) { // if statement OPEN

            // victory

            cout <<"Your have survived the onslaught!" << endl;

    } // if statement CLOSED

    else { // else statement OPEN

            // LOST

            cout << "You did not survive the zombie war" << endl;

    } // else statement CLOSED

    cout << "Zombies killed: " << zombiesKILLed << endl;
    cout << "Final score: " << playerScore << endl << endl;


} // main function CLOSED

Upvotes: 0

Views: 332

Answers (1)

ObliteratedJillo
ObliteratedJillo

Reputation: 5166

Splitting the command into two parts for easy understanding:

Part I-

rand() % 67 
= generate any random number from 0-66 
= remainder of division of any number by 67
= N (say)

Part II

if( rand() % 67 < 10)
= if( N < 10)

Upvotes: 1

Related Questions