Random number function returns mostly ones

I'm making a lottery guessing game where the user guesses a random number between 1 and 100. 90% of my code is working but my random number function keeps returning one as the answer. Can anyone shed some light as to why this keeps happening?


// draw num function
int DrawNum (int max) /* the maximum number to generate */
{ /* function drawNum */

double x = RAND_MAX + 1.0; /* x and y are both auxiliary */
int y; /* variables used to do the */
/* calculation */

y = static_cast<int> (1 + rand() * (max / x));
return (y); /* y contains the result */

} /* function drawNum */
You probably didn't seed the random number generator properly.
srand should be called only once in the entire program (usually at the beginning of main).

It works for me:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cstdlib>
#include <ctime>

// draw num function
int DrawNum (int max) /* the maximum number to generate */
{ /* function drawNum */

    double x = RAND_MAX + 1.0; /* x and y are both auxiliary */
    int y; /* variables used to do the */
    /* calculation */

    y = static_cast<int> (1 + std::rand() * (max / x));
    return (y); /* y contains the result */

} /* function drawNum */

int main()
{
    std::srand(std::time(nullptr));
    for (int i = 0; i < 5; ++i)
        std::cout << DrawNum(100) << ' ';
}
95 60 30 98 94
Last edited on
1
2
3
4
5
int DrawNum (int max)	/* the maximum number to generate */
{	/* function drawNum */

double x = RAND_MAX + 1.0;	 /* x and y are both auxiliary */
(1 + rand() * (max / x));


You can simply do:

1
2
3
rand() % 100 + 1; //0-99 then add 1 = 1 - 100
//formula:
rand() % (max - min + 1) + min;


Last edited on
Thanks! One more issue I'm having my code isn't passing a variable properly. The program is supposed to be a betting game where the user starts with 1000 dollars and guesses a number between one and one hundred. For some reason even though I used call by reference the money variable is not carrying through multiple games. I tried changing the variable money to the number 1000 but it just creates a whole host of other errors! If someone could help me diagnose this issue I would be very grateful as it's the final hitch in my game!


/*
Summary
This program is a guessing game. The user begins with $1000 and places bets
*/

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;
void PrintHeading(int);
int GetBet (int money, int& bet);
int GetGuess();

bool PlayAgain(void);
int PlayGame(int money);
int DrawNum (int max);

int main ()
{
int money=1000;


PlayGame(money);
while (PlayAgain()){
PlayGame(money);
}




return 0;
}
/*
PrintHeading simply prints the introductory output.
Parameters: initial amount of money received
*/
void PrintHeading(int money)
{
cout << "=============================================" << endl;
cout << " Welcome to the numbers betting Game. " << endl;
cout << " You have $" << money << " to begin game. " << endl;
cout << " Valid guesses are number between 1 and 100. " << endl;
cout << "=============================================" << endl << endl;
}
/*
GetBet prompts for and reads in a bet. The function performs all
error checking necessary to insure that a valid bet is read in
and does not return until a valid bet is enetered.
Parameters:
Money: the amount of money player has
Bet: The bet chosen by the user
*/
int GetBet(int money,int &bet){

cout <<"enter bet:";
cin >> bet;
if (bet > money || !cin || bet < 0){
cout << "Valid bet must be entered";
}
else{

return bet;
return money;
}



}

/*

GetGuess reads in a guess. The user is not prompted for the guess in
this function. The user only gets one chance to input a guess value.
Return Value: the value of the guess if the input is valid
0 if the input guess was not valid
*/

int GetGuess(int guess){

if (guess<0 || guess>100){
cout << "Valid guesses are numbers 1 through 100";
return 0;
}
else {
return guess;
}
}
/*

CalcNewMoney determines the amount of money the player has won or
lost during the last game.

Parameters:
money: the amount of money the player had going into the game
bet: the amount the player bet on the current game
guesses: the number of guesses it took the player to win.

-1 if the player did not guess correctly

Return Value: the new amount of money the player has

*/
int CalcNewMoney(int& money, int bet, int guesses)
{
if (guesses >6){
money= money - bet ;

}

if (guesses <7){
money = money + (bet/guesses);

}

return money;

}

/*

PlayAgain prompts the user to play the game again and reads in a response,
using a single character to represent a yes or no reply.
Error checking is performed on that response.

Return Value: true if the user wants to play again

false if the user does not want to play again.

*/

bool PlayAgain(void){

char playagain;
cout <<"Do you wish to play again?"<<endl;
cout <<"Enter Y or N" <<endl;
cin >> playagain;

if (playagain =='y' || playagain =='Y' ){
return true;
}
else{
return false;

}

}




// draw num function
int DrawNum (int max) /* the maximum number to generate */
{ /* function drawNum */

double x = RAND_MAX + 1.0; /* x and y are both auxiliary */
int y; /* variables used to do the */
/* calculation */

y = static_cast<int> (1 + rand() * (max / x));
return (y); /* y contains the result */

} /* function drawNum */

/*

PlayGame plays a single game, performing all the necessary calculations,
input, and output.

Parameters:

money: the amount of money the player has at the start of the game.

Return Value: how much the player has after the game.

*/


int PlayGame(int money){
int bet=0;
int guess;
PrintHeading(money);


GetBet(money, bet);
int attempt;
int real=DrawNum (100);
for (attempt=1; attempt <7; attempt++){
cout <<endl <<"Input Guess " <<attempt <<":";
cin >> guess;
int check = GetGuess(guess);
if (check != guess || guess <0 || guess > 100){
cout <<"Invalid Guess try a number between 1 and 100."<<endl;
cin >> guess;
}
if (guess == real ){
int cash_on_hand= CalcNewMoney(money,bet, attempt);
cout <<endl <<"You guessed right!" <<endl ;
cout <<"you have $" <<cash_on_hand <<" dollars left. ";

break;
}
if (guess != real) {
int cash_on_hand=CalcNewMoney(money, bet, attempt);}
if (guess<real){
cout <<endl << "Guess too low" <<endl ;
}
if (guess>real){
cout <<endl << "Guess too high" <<endl ;
}
}






return money;
}

Topic archived. No new replies allowed.