Part 29. Problem with rand()

Hi everyone!
I’m trying to recreate random generated numbers from the lesson, but every time I star a game I have the same numbers.
Where I wrong?

#include <iostream>>

#include <ctime>>

void PrintIntroduction(int Difficulty)

{

    std::cout << "\n *** Koroche ti otgadivaesh cifri slognostiu: " << Difficulty;

    std::cout << "\n *** Esli ti ne ugadaesh chisla, to tebe pizda \n\n";

}

bool PlayGame(int Difficulty)

{

    PrintIntroduction(Difficulty);

    const int CodeA = rand() % Difficulty + Difficulty;

    const int CodeB = rand() % Difficulty + Difficulty;

    const int CodeC = rand() % Difficulty + Difficulty;

    const int CodeSum = CodeA + CodeB + CodeC;

    const int CodeProduct = CodeA * CodeB * CodeC;

    

    std::cout << std::endl;

    std::cout << "+  Tut tri chisla nachui";

    std::cout << "\n+  Summa ravna: " << CodeSum;

    std::cout << "\n+  Esli chisla peremnogiti, to rezultat raven: " << CodeProduct << std::endl;

    int GuessA, GuessB, GuessC;

    std::cin >> GuessA >> GuessB >> GuessC;

    int GuessSum = GuessA + GuessB + GuessC;

    int GuessProduct = GuessA * GuessB * GuessC;

    std::cout << "###  Summa tvoich chisel:             " << GuessSum << "\n";

    std::cout << "###  Proizvedeniye tvoich chisel:     " << GuessProduct << "\n\n";

    if (GuessSum == CodeSum && GuessProduct == CodeProduct)

    {

        std::cout << "!!! Poluchaetsa c= !!!";

        return true;

    }

    else

    {

        std::cout << "!!! Ne poluchaetsa =c !!!";

return false;

    }

}

int main()

{

    srand(time(NULL));

    int LevelDifficulty = 1; 

    int const MaxLevel = 5;

    while (LevelDifficulty <= MaxLevel) //igra v zalupe poka vse urovni ne budut vipolneni

    {

        bool bLevelsComplete = PlayGame(LevelDifficulty);

        std::cin.clear();

        std::cin.ignore();

        if (bLevelsComplete)

        {

            ++LevelDifficulty;

        } 

        

    }

     std::cout << "\n\n*** Nu chego, pozdravlayu nachoi! *** ";

    return 0;

}

You’re starting with a value of 1. Any number % 1 is 0 which you then add 1 to.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms