Random Number
2002-11-10 17:25:54
Category: cpp:general
Description: Random number generation using srand(),rand() and time().
Author: detour
Viewed: 6070
Rating: (36 votes)


/* rand.cpp by detour@metalshell.com
 *
 * Generate a random number
 *
 * http://www.metalshell.com/
 *
 */
 
#include <iostream>
#include <cstdlib>
#include <time.h>
 
using namespace std;
 
int main() {
  /* Initialize the random number generator with a seed */
  srand((unsigned)time(NULL));
 
  int rnda = int((double(rand())/RAND_MAX)*50);
  int rndb = int((double(rand())/RAND_MAX)*75) + 25;
 
  cout << "A number between 0 and 50: " << rnda << endl;
  cout << "A number between 25 and 100: " << rndb << endl;
 
  return 0;
}