0

Hello I need help writing my program. I am trying to figure out how I should make it so when I type two range numbers (one is a maximum and one is a minimum) which the program will recognize and output a random number in between one of them. So far I have figured it out but it works only for whole numbers, I cant seem to find a way for those numbers to be with precision of 2 lets say.

#include "pch.h"
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using std::cout;
using std::cin;
using std::endl;

int main()
{
int  a, b, c, d;
srand(time(0));
cout << "Welcome to the game." << endl;
cout << "Please enter number range" << endl;
cout << "Highest number:";
cin >> c;
cout << "Lowest number:";
cin >> d;
b = d + (rand() % (c - d + 1));
cout << "Enter your guess:";
cin >> a;
unsigned int count = 0;
while (a < b) {
    cout << "Your number is lower than the correct one." << endl;
    cout << "Please try again:";
    cin >> a;
    count++;
}
while (a > b) {
    cout << "Your number is higher than the correcnt one." << endl;
    cout << "Please try again:";
    cin >> a;
    count++;
}

cout << "Your number is the correct one!!! You guessed it on " << count << " time" << endl;
return 0;

}
Mitko
  • 11
  • 1
  • 3
    Possible duplicate of [C++ random float number generation](https://stackoverflow.com/questions/686353/c-random-float-number-generation) – Rhathin Oct 08 '18 at 15:13
  • 1
    Note that your program doesn't do what you probably think it does. If you guess too high and then too low, it will tell you that you guessed correctly. – Max Langhof Oct 08 '18 at 15:13
  • This doesn't address the question, but do you really need the extra stuff that `std::endl` does? `'\n'` ends a line. – Pete Becker Oct 08 '18 at 15:16
  • Forgot to mention that I am a beginner, I have just started studying c++ yesterday. Every suggestion would be a good thing for me. – Mitko Oct 08 '18 at 15:19
  • C++ does not have a "precision of 2" data type, as it does integer types. You should read up on C++ floating point types, and possibly on decimal type libraries. It's not clear from your question which you are pursuing, so I am not closing this as the proposed duplicate. – Drew Dormann Oct 08 '18 at 15:22
  • https://en.cppreference.com/w/cpp/numeric/random/uniform_real_distribution – pptaszni Oct 08 '18 at 15:26
  • BTW, floating point variables are stored internally with maximum precision. You can *output* them with whatever precision you want. If you want the values stored with a given precision, you'll have to truncate. – Thomas Matthews Oct 08 '18 at 16:42

1 Answers1

1

You can use std::uniform_real_distribution to generate random floating point:

std::random_device device;
std::mt19937 generator(device());
std::uniform_real_distribution<> distribution(least, greatest);
double a_random_number = distribution(generator);

I cant seem to find a way for those numbers to be with precision of 2 lets say.

Simply round the result to nearest value.

But be aware that typical floating point representations cannot precisely represent all decimal numbers such as 0.1 etc.

eerorika
  • 232,697
  • 12
  • 197
  • 326