Hello . . I'm trying to get my random number generator to work but i'm having a tough time

here's what i've written so far

I can't seem to get the number generator to give me a truly random number . . . i'm sick of tooling with it and need a solution

Also . . .when i get it to work , it always generates the same number.

if I go through the game i've set up, and I guess the random number in 10 tries, it replays if you get it right . .but if you use up all your guesses, it just exits . . I can't seem to solve this problem either.

// C++ 1 Lab Final

#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
 
 
void replay();
int random();
int game(int guess, int rad_num);
int seed = rand();

void main(){
 int guess;
 int rad_num = random();
 cout<<"Guess a number between 1 and 100 ";
 cin >> guess;
 game(guess, rad_num);
 
 cout <<"The random number is \n"<<rad_num<<endl;
}

random(){
 srand(time(NULL));   // I need Help here generating a number
 
 int i;
 int rad_gen;
 int rad_res;
 for (i=0; i<rand() % 100;i++){
 rad_gen = (rand());
 rad_res = rad_gen % 100;
 }
return rad_res;
}

game(int guess, int rad_num){
 int guess_count;
  for (guess_count=10; guess_count>0; --guess_count){
  if (guess == rad_num){
   cout<<"You guessed right. "<<endl; 
   replay();
   
  }
   
  else if (guess < rad_num){
   cout<<"Too low \n"<<guess_count<<" guesses left."<<endl;
   cout<<"guess again ";
   cin>>guess;
  }
    else {
     cout<<"Too high \n"<<guess_count<<" guesses left."<<endl;
     cout<<"guess again ";
     cin >> guess;
     
   }
    if (guess_count == 0)
cout <<"Sorry you used all your guesses \n ";    // This doesn't work
replay();
}
   
  }
 
return 0;
}
 
void replay(){
int play;
cout<<"Play again? 1 for yes, 2 for no \n";
cin >> play;
 
 if (play == 1){
 main();
 
}
 else{
 cout<<"Good Bye";
 exit(0);
}
}

srand(time(NULL)); // I need Help here generating a number

u have to #include <time.h> in order to make this work

if (guess_count == 0)
cout <<"Sorry you used all your guesses \n "; // This doesn't work
replay();
}

First of all u forget a bracket, also u need to replace the 0 by 1:

if (guess_count == 1)
{
cout <<"Sorry you used all your guesses "<<endl; 
replay();
}

the random number generator (srand) usually uses time as its string. the
time(NULL) is held in the ctime.h library and it takes i think the ammount of seconds from some date in the 70's and does an equation with it. the number isnt completely random but it does a good job making it look like it. after including the ctime.h library initialize the generator by srand(time(NULL)); then you can make a number between two numbers by this equation:

lowerbound + rand() % (upperbound - lowerbound + 1);

where the lowerbound is the lower number and the upperbound is the higher number.
Code Sample:

#include <iostream>
#include <ctime.h>
#include <cstdlib.h>
 
using namespace std;
 
int main()
{
     srand(time(NULL));
     cout << 1 + rand() % (10 - 1 + 1)
     system("pause");
     return 0;
}

^^^^my little tutorial of random numbers...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.