Generating Random number from input range

I am not sure how to write this program. Any help is needed as I am stuck and this program is due soon. I am very very new to c++ so looking at other similar programs is of no help
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
Section 0 : A function that generates a random number of a given range.

Section 1 : Receive the range for the random number.

Section 2 : Receive how many random numbers to be generated.

Section 3 : Generates random numbers of the given range.

*/

#include <iostream>

#include <ctime>

using namespace std;

int myrand(int min, int max)

{

	 

}

int main()

{

	srand(time(NULL)); // It sets the random number seed.



	cout << "Input minimum number:";

	cin >> min;

	cout << "Input maximum number:";

	cin >> max;



	return 0;

}
Could you give me some example code of where to begin here
(rand() % (max - min + 1)) + min
So I now have
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <ctime>
using namespace std;
int myrand(int min, int max)
{
	int x = min + rand() % ((max + 1) - min);
	return x;
}
int main()
{
	int min;
	int max;
	cout << "Input maximum number:";
	cin >> max;
	cout << "Input minimum number:";
	cin >> min;

	srand(time(NULL)); // It sets the random number seed.		}
	


	return 0;
}


Am I going in the correct direction?
Last edited on
Could you give me some example code of where to begin here

The link I put up has example code.
Still not sure if i am heading in the correct direction here.
Still stuck on this if someone. can provide insoght
In principle, the answer is 'yes'.

However, if your intention is to learn to use random numbers with C++, then the answer is 'no'.

Here is a different example program: http://www.cplusplus.com/reference/random/uniform_int_distribution/
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
28
29
#include <iostream>
#include <cstdlib>  // srand / rand
#include <ctime>    // time

int myrand(int min, int max);

int main()
{
   srand(time(nullptr));

   std::cout << "Input minimum number: ";
   int min { };
   std::cin >> min;

   std::cout << "Input maximum number: ";
   int max { };
   std::cin >> max;

   std::cout << "\nA random number: " << myrand(min, max) << "\n\n";

   int ran_num { myrand(min, max) };

   std::cout << "Another random number: " << ran_num << '\n';
}

int myrand(int min, int max)
{
   return (rand() % (max - min + 1)) + min;
}

Input minimum number: 5
Input maximum number: 20

A random number: 16

Another random number: 13

C++ method for obtaining random numbers:
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
28
29
30
31
#include <iostream>
#include <random>

int myrand(int min, int max);

int main()
{
   std::cout << "Input minimum number: ";
   int min { };
   std::cin >> min;

   std::cout << "Input maximum number: ";
   int max { };
   std::cin >> max;

   std::cout << "\nA random number: " << myrand(min, max) << "\n\n";

   int ran_num { myrand(min, max) };

   std::cout << "Another random number: " << ran_num << '\n';
}

int myrand(int min, int max)
{
   // create a random engine that exists between function calls
   static std::default_random_engine rng(std::random_device{}());

   std::uniform_int_distribution<int> dis(min, max);

   return dis(rng);
}

Input minimum number: 5
Input maximum number: 20

A random number: 11

Another random number: 13


Using the C library random functions in C++ code is not recommended.

https://web.archive.org/web/20180123103235/http://cpp.indi.frih.net/blog/2014/12/the-bell-has-tolled-for-rand/

https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful

C++ has much better means to generate random numbers.

https://www.learncpp.com/cpp-tutorial/59-random-number-generation/
Topic archived. No new replies allowed.