Random Number Generator

Published on: August 16, 2010

This is another new thing that you'll learn in C++ today. Today I show you how to create a program that acts as a random number generator and gives you a random number based on what the user inputs as the maximum value for the random number. So whether the number is 5 or 5,000,000, the program will be able to give you a random value. This is great practice for beginners with C++ and this tutorial should help further your knowledge about the basics of C++.

Remember to checkout the Codesnip below to review the C++ code yourself! Feel free to copy the code, but I ask that you please provide credit if you leave the code unchanged when you use it.

Codesnip:

//Programmer: Nazmus
//Program:  Random Number
//Website:  EasyProgramming.net

#include <iostream>
#include <ctime> 
#include <cstdlib>
using namespace std;

int main()
{
 int max, random_number, i;
 for(i=1;i<=5;i=i+1){
 cout << "Please input max integer: ";
 cin >> max;

 srand(time(0));
 random_number = (rand () % max) + 1;

 cout << random_number << endl;
 }
 system("pause");
}



Comments: