<<< Copy algoritm example     Index     Transform algoritm example >>>

11. Pseudo-random numbers

  • 
    #include <iostream>
    #include <cstdlib> // for rand() and srand()
    #include <ctime>
    
    int main()
    {
        srand( (unsigned)time( 0 ) );
        for(;;) {
            std::cout << rand() % 10;
            std::cout << '\t';
        }
        return 0;
    }
    
    
  • time function returns the number of seconds elapsed since midnight, January 1, 1970.

  • srand will seed new sequence of pseudo-random numbers, so that the numbers will be different every time we run.
    (The initial seed is 1.)

  • rand returns an integer pseudo-random number.


<<< Copy algoritm example     Index     Transform algoritm example >>>