1: //random_functions.cpp
2: //Illustrates the system random number generator (rand() and srand()).
4: #include <iostream>
5: #include <cstdlib>
6: #include <ctime>
7: using namespace std;
9: int main()
10: {
11: cout << "\nThis program illustrates the basic utility functions "
12: "used for generating a\nsequence of \"pseudorandom\" values of "
13: "one type or another in C++. It shows\nthree system functions "
14: "that are available from the standard libaries: time()\nfrom "
15: "<ctime>, plus rand() and srand() from <cstdlib>. The time() "
16: "function\nis included because it is very helpful for obtaining "
17: "a \"random seed\" value\nfor use with the other two functions.";
18: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
20: cout << "\nThe time function from the <ctime> "
21: "library returns a value of type time_t, \n"
22: "which is an integer representing the "
23: "current time as a number of seconds, \n"
24: "provided the function is called with argument 0.";
25: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
27: cout << "\nCurrent value of function call time(0) is " << time(0) << ".";
28: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
30: cout << "\nRAND_MAX is a named constant from <cstdlib>.";
31: "\nIt is the largest possible return value "
32: "from a call to the rand() function.";
33: cout << "\nValue of RAND_MAX on this system: " << RAND_MAX;
34: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
36: cout << "\nHere are 10 \"random\" integer values "
37: "in the range 0.." << RAND_MAX << ": \n";
38: for (int i=1; i<=10; i++)
39: cout << rand() << " ";
40: cout << "\nThe above values will be the same each time this "
41: "executable is run.";
42: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
44: srand(time(0));
45: cout << "\nHere are another 10 \"random\" integer values "
46: "in the range 0.." << RAND_MAX << ": \n";
47: for (int i=1; i<=10; i++)
48: cout << rand() << " ";
49: cout << "\nThe above values will be different each time this "
50: "executable is run.";
51: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
53: int small, large;
54: cout << "\nEnter two positive integers, with "
55: "the second larger than the first: ";
56: cin >> small >> large; cin.ignore(80, '\n'); cout << endl;
57: cout << "Here are 10 random integer values between "
58: << small << " and " << large << " (inclusive): \n";
59: for (int i=1; i<=10; i++)
60: cout << small + rand() % (large - small + 1) << " ";
61: cout << "\nThe above values will also be different each time this "
62: "executable is run.";
63: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
65: cout << "\nEnter two more positive integers, with "
66: "the second larger than the first: ";
67: cin >> small >> large; cin.ignore(80, '\n'); cout << endl;
68: cout << "Here are 10 random integer values between "
69: << small << " and " << large << " (inclusive): \n";
70: srand(15);
71: for (int i=1; i<=10; i++)
72: cout << small + rand() % (large - small + 1) << " ";
73: cout << "\nThe above values will be the same each time this "
74: "executable is run,\nprovided the same two input values "
75: "are used.";
76: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
77: }