demo-random - cout <<...

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() { int rand_int; // our random number // Generate a random number without restrictions srand( (unsigned) time(0) ); // set seed for random number generation rand_int = rand(); // 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 &lt;&lt; &quot;Random #: &quot; &lt;&lt; rand_int &lt;&lt; endl; // Alternatively. .. generate a random number within a given range // Let's say we want a number between 0 and 100 rand_int = rand() % 101; // use mod to trim the number cout &lt;&lt; &quot;Random # between 0 and 100: &quot; &lt;&lt; rand_int &lt;&lt; endl; // Generate several random numbers. .. cout &lt;&lt; &quot;Several random #s between 0 and 100: &quot; &lt;&lt; endl; for(int i = 1; i &lt;= 50; i++) { cout &lt;&lt; rand() % 101 &lt;&lt; &quot; &quot;; } cout &lt;&lt; endl; // How could you generate a random number between, say, 50 and 100? return 0; }...
View Full Document

Ask a homework question - tutors are online