Computer Science Canada

Generating a random number in C++

Author:  Miko99 [ Wed May 21, 2003 9:47 am ]
Post subject:  Generating a random number in C++

How do i generate a random number in c++?

Author:  Catalyst [ Wed May 21, 2003 2:26 pm ]
Post subject: 

rand()

Author:  octopi [ Wed May 21, 2003 3:49 pm ]
Post subject: 

This should demonstrate whats going on.
srand seeds the random number generator (makes numbers random)

% 10 means mod 10.
it will make it so
rand() %10 will output numbers between 0-9

code:
#include <iostream.h>
#include <stdlib.h>
#include <time.h>


int main() {
  srand(time(0));
  for (int p=0;p<30;p++) {
    cout << rand() % 10 << " ";
  }
  return 1;

}


: