This blog is under construction

Monday, 30 April 2012

srand example in C

Header file:

     void srand(unsigned int val);

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

srand function C example:

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

  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