I'm fairly new to using C++, but I'd like to make a game like Krypto. To start off, I just need to learn how to make int a - f be a random number between 1 and 100.

#include <iostream>
#include <string>
using namespace std;

int main ()

 int a; 
 int b; 
 int c; 
 int d; 
 int e; 
 int f;
 string mystr;

a = 5;
b = 10;
c = 15;
d = 20;
e = 25;
f = 30;

cout << "Are you ready to play Krypto \n \n";
getline (cin, mystr);
cout << "\nThen let's play! \n" << a << ", " << b << ", " << c << ", " << d << ", " << e << ", Should Equal..\n" << f << "\n";
cout << "\nUse order of operations to solve, then Input the answer you find. \n \n";
getline (cin, mystr);
return 0;     

so, simply, I want a, b, c, d, e, and f be a random number each time.
How would I accomplish this?

9 Years
Discussion Span
Last Post by GDICommander

Use rand() function. If you want truly random values at each program execution, use srand() function and put time() (returns the time in seconds since a given start moment) as the parameter for srand().

Again, look for other references for more info about these functions.


How would I make it so that rand() is just 1-100?
(i tried doing rand(1-100), too no avail.)


Her is an example,

#include <ctime>

int a = rand() % 10 +1;

this code stores a value between 1and 10 :)



The modulo operator (%) returns the remainder of integer division.

So, if you want a random number between A and B, you just call rand() and you do A + rand() % B. Note that this formula excludes B as a possible result.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.