Jump to content

just a simple guessing game

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
deryni

deryni

    Newbie

  • Members
  • Pip
  • 1 posts
I am wondering if there is some code that lets you creat a number one through ten randomly? any help here would be appreciated

#2
Termana

Termana

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,057 posts
I'm assuming your using C++.
In your includes make sure you have:
#include <cstdlib>
#include <ctime>

And use this function, it will produce a random number between 1 and 10 when called:

int randfunction()
{
    std::srand(time(0));
    return std::rand() % 10 + 1;
}

Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!


#3
shibbythestoner

shibbythestoner

    Programmer

  • Members
  • PipPipPipPip
  • 135 posts
#include <iostream>
#include <ctime>

using namespace std;

int getRandomNumber(int min, int max)
{
  int range = (max - min) + 1;
  int number = min + (int)(range * rand() / (RAND_MAX + 1.0));
  return(number);
}

int main(void)
{
  srand(time(0));
  int answer = getRandomNumber(1,10);
  int input = 0;
  cout<<"I am thinking of a number between 1 and 10. Can you guess it?"<<endl;
  while(input != answer) {
    cout<<"Guess: ";
    cin>>input;
    if(input == answer) {
      break;
    }
    cout<<"Wrong! Try again."<<endl;
  }
  cout<<"Correct! "<<answer<<" was the number I thought of."<<endl;
  cin.get();
}

Posted Image