This blog is under construction

Monday 30 April 2012

srand example in C

Header file:
    stdlib.h

Synopsis:
     void srand(unsigned int val);

Description:
      It uses the value val as the seed to generate a new sequence of pseudo random numbers.


srand function C example:


  #include<stdio.h>
  #include<stdlib.h>
  int main() {
        int randomno, i, n, seed = 1;
        printf("Enter the no of random numbers you want to generate:");
        scanf("%d", &n);
        srand(seed);
        for(i = 1; i < n; i++) {
                printf("Random no %d: %d\n", i, rand());
        }
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter the no of random numbers you want to generate:5
  Random no 1: 1804289383
  Random no 2: 846930886
  Random no 3: 1681692777
  Random no 4: 1714636915



No comments:

Post a Comment