Generating Random Numbers using rand - C++ Data Type

C++ examples for Data Type:Random

Description

Generating Random Numbers using rand

Demo Code

#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

double doubleRand() {
  return double(rand()) / (double(RAND_MAX) + 1.0);
}

int main() {//w w w. j a v a2s . c  o  m
  srand(static_cast<unsigned int>(clock()));

  cout << "expect 5 numbers within the interval [0.0, 1.0)" << endl;
  for (int i=0; i < 5; i++) {
    cout << doubleRand() << "\n";
  }
  cout << endl;
}

Result


Related Tutorials