Monday, 2 December 2013

c program to generate random numbers

C programming code using rand

#include <stdio.h>
#include <stdlib.h>
 
int main() {
  int c, n;
 
  printf("Ten random numbers in [1,100]\n");
 
  for (c = 1; c <= 10; c++) {
    n = rand()%100 + 1;
    printf("%d\n", n);
  }
 
  return 0;
}

C programming code using random function(Turbo C compiler only)

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
 
main()
{
   int n, max, num, c;
 
   printf("Enter the number of random numbers you want ");
   scanf("%d",&n);
 
   printf("Enter the maximum value of random number ");
   scanf("%d",&max);
 
   printf("%d random numbers from 0 to %d are :-\n",n,max);
   randomize();
 
   for ( c = 1 ; c <= n ; c++ )
   {
      num = random(max);
      printf("%d\n",num);
 
   }
 
   getch();
   return 0;
}

No comments:

Post a Comment