Using C++, how to randomly generate either one, two or three digits.

Using C++, how to randomly generate either one, two or three digits.
-----------------------updated 2020/3/17-------------------------------
Thanks so much for helping me !! I've seen all the comments.
And what I mean is how to randomly generate an integer.
So when it "cout", the integer should either be 1 digit , 2 digits or 3 digits.
Actually it's part of following question. Please take a look.

Question:
Modify SubtractionQuiz.cpp so that it will become a SimpleMathematicalQuiz.cpp that has the following features:
i) A text menu will be displayed below to ask user to select an option: 1) Addition, 2) Subtraction, 3) Multiplication, 4) Exit.
ii) Then, after the user select option 1, 2 or 3, it will prompt user to input the number of operands (Maximum is 4 and minimum is 2).
iii) Then, the program will generate random numbers for the operands, which should be either one, two or three digits.
iv) It will display the formula and ask user to input the guessing answer.
v) Then, the program will evaluate whether the guessing answer is correct. If correct, state “It is correct.”; otherwise, state “It is wrong.” and give correct answer.
vi) Then, it will go back to i) to show text menu again which prompts user to select an option.
vii) At the end, if user selects option 4, it will show the total number of questions answered; the number of answers correct and wrong respectively and the mark (number of answers correct / total number of questions answered * 100).

Sample output: (Contents in bold type are input values)
1. Addition
2. Subtraction
3. Multiplication
4. Exit
Please select an option: 1
How many operands: 3
34 + 8 + 126
Please input your answer: 180
It is wrong! The answer should be 168

1. Addition
2. Subtraction
3. Multiplication
4. Exit
Please select an option: 3
How many operands: 2
56 * 28
Please input your answer: 1568
It is correct!

1. Addition
2. Subtraction
3. Multiplication
4. Exit
Please select an option: 2
How many operands: 3
86 – 7 - 139
Please input your answer: -50
It is wrong! The answer should be -60
Last edited on
Only numbers from 0 to 999 (inclusive) have one to three digits.
std::uniform_int_distribution<int> distribution( 0, 999 );

See http://www.cplusplus.com/reference/random/uniform_int_distribution/
@LoMoyi

Are you wanting to generate up to 3 different 1 digit numbers, or a 1 digit, 0 to 9, 2 digit, 10 to 99, or 3 digit, 100 to 999, number, maybe by user input?
Last edited on
0-999 only gives 10 / 1000 chances for a 1 digit value.
if you want to normalize that chance, you need to tell us.
The question is too vague. Can you elaborate?

I like jonnin's idea of equalizing the chances of a 1, 2, or 3 digit number.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <iomanip>
#include <random>

int f()
{
    using IntDist = std::uniform_int_distribution<int>;

    static std::minstd_rand rnd{ std::random_device{}() };
    static IntDist
        digits( 0, 2 ),
        dist[3]
        {
            IntDist{   0,   9 },
            IntDist{  10,  99 },
            IntDist{ 100, 999 }
        };

    return dist[ digits( rnd ) ]( rnd );
}

int main()
{
    for (int i = 0; i < 100; ++i)
        std::cout << f() << ' ';
    std::cout << '\n';
}

Last edited on
"I need X digits" is a vague statement. It can be integer digits or decimal digits.

So far there have been good integer solutions, ones that could be adapted for decimal digits as needed.

@LoMoyi, is this post related to your other post? http://www.cplusplus.com/forum/beginner/268812/

That post deals with floating point numbers.
Topic archived. No new replies allowed.