Prevent a randomised integer from constantly being reassigned
<p>I'm working on a small game that chooses a number at random, and then asks the user to guess it, giving indications based on the user's choice (bigger, smaller...). To generate a random number between 0 and 100, I'm using the following code:</p>

<pre><code>int random = rand() % 101;
</code></pre>

<p>This works fine, but the only problem is that this variable is constantly being reassigned a different value. How can I prevent this?</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;cmath&gt;
using namespace std;
int tries;//number of tries allowed
bool win;//whether the user has won the game or not
int main(){
    string gameType;
    cout &lt;&lt; "Please enter your game type; \"easy\", \"medium\", \"hard\", and \"expert\"." &lt;&lt; endl;
    cin &gt;&gt; gameType;
    if(gameType == "easy"){
        tries = 40;
        cout &lt;&lt; "You have 40 tries." &lt;&lt; endl;
    }else if (gameType == "medium"){
        tries = 20;
        cout &lt;&lt; "You have 20 tries." &lt;&lt; endl;
    }else if (gameType == "hard"){
        tries = 10;
        cout &lt;&lt; "You have 10 tries." &lt;&lt; endl;
    }else if (gameType == "expert"){
        tries = 5;
        cout &lt;&lt; "You have 5 tries.";
    }

    cout &lt;&lt; "Please enter a number between 0 and 100." &lt;&lt; endl;

    while (win == false){
        int random = rand() % 101;//random number generation between 0 and 100
        return(random);
        bool valid = false;//if the user's number is valid (0 &gt;= ; &lt;= 100)
        int usedTries = 0;//the tries that the user has used up
        int selection;
        cin &gt;&gt; selection;
        if (selection &gt; 100){
            cout &lt;&lt; "Your number cannot be above 100." &lt;&lt; endl;
            valid = false;
        }else if (selection &lt; 0){
            cout &lt;&lt; "Your number cannot be below 0";
            valid = false;
        }else{
            valid = true;
        }
        if (valid == true){
            if (selection &gt; random){//bigger than target number
                cout &lt;&lt; "Smaller!" &lt;&lt; endl;
                usedTries = usedTries + 1;
            }else if (selection &lt; random){//smaller than target number
                cout &lt;&lt; "Bigger!" &lt;&lt; endl;
                usedTries = usedTries + 1;
            }else if (selection == random){//the user has guessed the right answer
                cout &lt;&lt; "Yay! You win!" &lt;&lt; endl;
                win = true;
            }
            if (usedTries &gt;= tries){//user has used up his number of tries
                cout &lt;&lt; "Sorry, you've lost the game. Try again later." &lt;&lt; endl;
                win = false;//to end the loop and terminate the game
            }
        }
    }
return(0);
}
</code></pre>
