demo-random-fake-drand - set seed for random number...

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 double NextRandNum() // PRE: random number generator has been initialized with a call to srand() // POST: FCTVAL == next random number >= 0 and < 1 { return double(rand()%32768)/32768; // generate "random" number } int main() { double rand_double; // our random number between 0 and 1 // Generate a random number without restrictions srand( (unsigned) time(0) );
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: // set seed for random number generation rand_double = NextRandNum(); // generate "random" number 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 = NextRandNum()*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 << NextRandNum()*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