Produce random values in the range of 0 to whatever number you specify. - C Data Type

C examples for Data Type:Random Number

Description

Produce random values in the range of 0 to whatever number you specify.

Demo Code

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int rnd(int range);
void seedrnd(void);
int main()/*from   ww  w .  j a  v  a2  s . c  o  m*/
{
   int x;
   seedrnd();
   for(x=0;x<100;x++)
      printf("%i\t",rnd(10));
   return(0);
}
int rnd(int range)
{
   int r;
   r=rand()%range;
   return(r);
}
void seedrnd(void)
{
   srand((unsigned)time(NULL));
}

Result


Related Tutorials