Wednesday, May 11, 2011

srand() code example c c++ objc

A simple random number generator based on rand() and srand() functions.
srand() function is used to initialize seed for random number.

Example - srand()

#include<stdlib.h>

// Create random number between 0 and 100.
int n = rand() % 100;

// Create random character between '0' and '9' in ASCII code.
srand(time(NULL));
 for(i=0;i<100;i++)  
 {
   char ch = rand() % (57-48+1)+48;
   putc(ch);
 }