demo-random-drand48 - cout<<"Random"<<...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
// Random number generation demo program #include <iostream> // for cin, cout, endl #include <cstdlib> // for random number generator #include <ctime> // for time method so we can seed the generator using namespace std; // needed if we use "iostream" instead of "iostream.h" // otherwise, we'd need "std::" in front of cin, cout, endl int main() { double rand_double; // our random number between 0 and 1 // Generate a random number without restrictions srand48( (unsigned) time(0) ); // set seed for random number generation rand_double = drand48(); // generate "random" number
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: cout << "Random #: " << rand_double << endl; // Alternatively. .. generate a random number within a given range // Let's say we want a number between 0 and 100 rand_double = drand48()*100; // use multiplier to scale the number cout << "Random # between 0 and 100: " << rand_double << endl; // Generate several random numbers. .. cout << "Several random #s between 0 and 100: " << endl; for(int i = 1; i <= 50; i++) { cout << drand48()*100 << " "; } cout << endl; // How could you generate a random number between, say, 50 and 100? return 0; }...
View Full Document

This document was uploaded on 02/10/2011.

Ask a homework question - tutors are online