You are here:

C++/program samples


Ok here are the programs I came up with.

// Hello World! Program
#include <iostream>
using namespace std;

int main ()
   cout << "Hello World!";
   return 0;

// Number Regurgitation Program!

#include <iostream>
using namespace std;

int main ()
   int i;
   cout << "Please enter a number: ";
   cin >> i;
   cout << "The number you entered is " << i;
   return 0;

I've gone through the C++ tutorials and even did a Google search and I'm afraid I can't figure out how to make the random number generator.

Hi, Tristen.

OK, the first two programs are exactly what I was looking for.  Well done.  One thing I will suggest: it's good practice to end your output lines with newline characters.  It isn't required, and, on some occasions, isn't even desirable, but mostly, it's a good idea to add a newline character at the end.  This can be accomplished in two ways with cout.  One is simply with the '\n' newline character itself, the other is with the endl tag.  Here are samples of each:

   cout << "Hello World!\n";
   cout << "Hello World!" << endl;

OK, as for the random number generator, it is a bit trickier than the other two programs.  It uses the same input and output logic as the other two programs, but I've added a new function into the mix, the rand() function.  Here again is the program definition:

Program 3 - Random numbers
  Write a program which asks the user for two numbers: one low number and one high number.  Then, generate a random number between those two numbers (including both as the extreme values).  Print the randomly generated number to the screen.  Output should be something like:

  Please enter a low number: 5
  Please enter a high number: 10
  Your random number is: 6

  Functions you will need to use:
      - cout
      - cin
      - rand
  Bonus points for:
      - srand
      - time

So, again, cout and cin are there.  The rand function is the new addition.  If you look up the reference on the rand function, you'll see that it is a simple function that takes no parameters and returns an integer between 0 and RAND_MAX.  You don't have to worry about what RAND_MAX is defined as, just understand that all numbers generated by the rand function will be in that range.  So, the following code will generate and store a number between 0 and RAND_MAX:

   int randInt = rand();

Now, if you make a program that generated 10 random numbers and prints them to the console, you will notice that it always generates the same 10 random numbers.  Not very random, eh?  The problem, of course, is that computers simply are not and cannot be random.  So, the "random" number generator is really an algorithm that generates a "pseudo random" number.  The thing is, since it's an algorithm, it always returns the same values.  Not very fun.  This is where the srand function comes into play.

The srand function seeds the random number generator, which means that it initializes the random number generator with a seed, or starting point.  The srand function takes in an integer, which it passes to the random number generator to use in the random number generation process.  So the trick, then, is to seed the random number generator with a different number every time you start your application.  This means that we need some method of generating a new number every time our application is run.  You could do something like start at 0, and increment the value every time you run your application, but this means that you will have to store your current seed to disk so that it may be accessed the next time you run your application.  A much simpler and very widely used method is to use the current time as the seed value.  This is where the time function comes into play.

The time function takes one parameter and returns a value that is the number of seconds elapsed since midnight, January 1, 1970.  The parameter is unimportant for our purposes, and can be assumed to always be 0.  The return value is of type "time_t", which we simply re-interpret as an integer.  So, to seed the random number generator with the current time and generate a random number, we would do this:

   int randInt = rand();

The one thing you may not have encountered before is the int keyword being used like it is in the srand line.  C++ has something called "type casting" where a variable type can be converted to another variable type.  Sometimes this is handled implicitly without any issue, but sometimes it will throw a compiler warning (rarely an error).  To avoid these warnings, and just to be a better coder, it is better to explicitly cast a variable to another type.  This is in one of a couple of ways:

   float myFloat = 2.5;
   int myInt1 = int(myFloat);  // Line 1
   int myInt2 = (int)myFloat;  // Line 2

Line 1 is the accepted C++ way of type casting.  It looks like a function call, which I think is why it's the preferred method in C++.  Line 2 is the C-style method of type casting.  Either way works, for some reason I find I prefer the second method.  Sometimes I even toss both in parenthesis, just for good measure:

   int myInt3 = (int)(myFloat);

So, there you go, I've now shown you the rand, srand, and time functions as well as explicit type casting.  One thing to note is that in order to use the time function, you must include the header <time.h>:

   #include <time.h>

Also, note that I didn't describe how to change your random number from 0 to RAND_MAX into a random number from a specified low to a specified high.  This is because I want you to try to figure that out on your own.  I will say that it requires a modulus operator (the % sign).  Read up on what the modulus operator is and does, and see if you can figure out how to convert the range from 0 - RAND_MAX into low - high.  I will say that, worse comes to worse, the algorithm is all over the place on the Internet.  It's incredibly wide spread, especially when generating random numbers within a specified range.

Good luck with the rest of problem 3, and let me know if you get stuck on it.  I look forward to seeing your completed solution and I'll get some more programs together for when you're done with problem 3.


All Answers

Answers by Expert:

Ask Experts


Joseph Moore


I've been programming in one form or another since my brother taught me BASIC when I was 6. I've been programing professionally since I was 20, first web development with HTML, JS, DHTML, CSS, etc., then I became a video game developer, writing code in C, C++, C#, SQL, assembly, and various scripting languages. I've even written my own scripting languages, custom designed for the games I was making. I also dabble in Java, PHP, and Perl. I've worked on pretty much every aspect of game development, including graphics, audio, gameplay, tool, UI, input, animation, and physics.


I've been writing C++ code for 12 years, both on my own in my spare time and professionally.


Bachelor of Science in Game Design and Development, Full Sail University, Winter Park, FL

Awards and Honors
Salutatorian and Advanced Achiever Awards at Full Sail; Independent Games Festival Student Showcase winner, 2004; Featured article on Gamasutra about an experimental game developed in 2004

©2016 All rights reserved.