Copyright © Programs ++
Design by Dzignine
Showing posts with label random numbers. Show all posts
Showing posts with label random numbers. Show all posts
Thursday, 19 January 2012

Program to generate random numbers

Random numbers in c++ are generated using functions such as rand() and srand() located in <cstdio> i.e. the C standard library. For those who use turbo c++, it you can find these functions in <stdio.h>.

// Program to generate random numbers
#include <iostream>
#include <cstdlib>

int main()
{
    int x;
    std::cout<<" Enter the max limit : ";
    std::cin>>x;
    srand(time(0));
    for (int i=0; i<20; i++)
    {
        std::cout<<rand()%x<<std::endl;

        }
    std::cin.get();
    return 0;
} // end of main