Home c++ Random number between A and B

# Random number between A and B

Author

Date

Category

I wrote a program in which the user can set the numbers A and B.

A is the minimum number.
B is the maximum number.

Ie in fact, this is a range of numbers, and the program should immediately output `X `– a random number from this range.

The problem is that compilers (tried it online and CodeBlocks) produce either the same number, or even exceed the maximum number – `B `.

I entered A = 10, B = 100, I got X = 93 with each repetition, sometimes more than 100.
What is the problem? P.s. I’ll leave 2 codes

``````#include & lt; iostream & gt;
#include & lt; stdio.h & gt;
#include & lt; cstdlib & gt;
using namespace std;
int main () {
int x, a, b;
printf ("Min number:");
cin & gt; & gt; a;
printf ("Max number:");
cin & gt; & gt; b;
printf ("Random number:");
cout & lt; & lt; x + rand ()% b + a;
}
#include & lt; iostream & gt;
#include & lt; stdio.h & gt;
#include & lt; cstdlib & gt;
using namespace std;
int main () {
int x, a, b;
printf ("Min number:");
cin & gt; & gt; a;
x = a + rand ()% b;
printf ("Max number:");
cin & gt; & gt; b;
printf ("Random number:");
cout & lt; & lt; x;
}
``````

``````a + rand ()% (b-a)
``````

This gives you values ​​in the range `[a, b) `.

If necessary up to `b `inclusive (`[a, b] `) –

``````a + rand ()% (b-a + 1)
``````

Each time the program is run, `rand () `produces the same sequence of random numbers. You can once call at the beginning of the program `srand (time (0)) `– to initialize the RNG with the current time value.

With the arrival of the C++ 11 standard, the `& lt; random & gt; `header file appeared, which allows you to explicitly generate a random number from a closed interval, and with a uniform distribution. The template class std :: uniform_int_distribution is used for this.

``````# include & lt; iostream & gt;
#include & lt; chrono & gt;
#include & lt; random & gt;
#include & lt; map & gt;
static std :: mt19937_64 gen {
static_cast & lt; unsigned long long & gt; (
std :: chrono :: system_clock :: now (). time_since_epoch (). count ())};
void show_stats (std :: uniform_int_distribution & lt; int & gt; dis) {
std :: map & lt; int, int & gt; counts;
int testCount = 1000 * 1000;
for (int i = 0; i & lt; testCount; i ++) {
counts [dis (gen)] ++;
}
for (const auto & amp; p: counts) {
std :: cout & lt; & lt; "P (" & lt; & lt; p.first & lt; & lt; "|" & lt; & lt; dis.min () & lt; & lt; "," & lt; & lt; dis.max () & lt; & lt; ") = "& lt; & lt; p.second * 1.0 / (testCount * 1.0) * 100.0 & lt; & lt; "%" & lt; & lt; std :: endl;
}
};
int main ()
{
int A = 1, B = 20;
std :: uniform_int_distribution & lt; int & gt; dis (A, B);
std :: cout & lt; & lt; "random number in closed interval [" & lt; & lt; A & lt; & lt; "," & lt; & lt; B & lt; & lt; "]:" & lt; & lt; dis (gen) & lt; & lt; std :: endl;
show_stats (dis);
}
``````

Output:

``````random number in closed interval [1,20]: 15
P (1 | 1.20) = 4.9692%
P (2 | 1.20) = 5.0339%
P (3 | 1.20) = 5.0239%
P (4 | 1.20) = 4.9947%
P (5 | 1.20) = 5.0186%
P (6 | 1.20) = 5.0049%
P (7 | 1.20) = 4.9935%
P (8 | 1.20) = 4.975%
P (9 | 1.20) = 5.0004%
P (10 | 1.20) = 4.9836%
P (11 | 1.20) = 5.0179%
P (12 | 1.20) = 4.9781%
P (13 | 1.20) = 5.0065%
P (14 | 1.20) = 4.9524%
P (15 | 1.20) = 4.9968%
P (16 | 1.20) = 5.0168%
P (17 | 1.20) = 5.0158%
P (18 | 1.20) = 5.0075%
P (19 | 1.20) = 4.9998%
P (20 | 1.20) = 5.0107%
``````

Random numbers in the C++ programming language can be generated by the rand () function from the C++ standard library. The `rand () `function generates numbers in the range 0 to `RAND_MAX `. `RAND_MAX `is a constant defined in the `& lt; cstdlib & gt; `library. For `MVS ``RAND_MAX `= 32767, but it can be more depending on the compiler. Below is a simple program using the `rand () `random number generator:

``````// random.cpp: defines the entry point for the console application.
#include "stdafx.h"
#include & lt; iostream & gt;
using namespace std;
int main (int argc, char * argv [])
{
cout & lt; & lt; "RAND_MAX =" & lt; & lt; RAND_MAX & lt; & lt; endl; // constant holding the maximum limit from the range of random numbers
cout & lt; & lt; "random number =" & lt; & lt; rand () & lt; & lt; endl; // start the random number generator
system ("pause");
return 0;
}
``````

In order to scale the number generation interval, you need to use the operation of finding the remainder of the division `% `.

``````// example of scaling the range of generating random numbers
rand ()% 3 + 1; // range is from 1 to 3 inclusive
``````

In your case, this will be:

``````// example of scaling the range of generating random numbers
a + rand ()% (b-a + 1); // range is from a to b inclusive
``````

That’s it!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.