Name

rand, srand
- pseudo-random number generator

Library

libc.lib

Synopsis

  #include <stdlib.h>
  void srand (unsigned seed);
  int rand (void);

Return values

The rand function never returns return the next pseudo-random number in the sequence.

The srand function never returns.


Detailed description

The rand function computes a sequence of pseudo-random integers in the range of 0 to RAND_MAX (as defined by the header file
  #include <stdlib.h>).

The srand function sets its argument seed as the seed for a new sequence of pseudo-random numbers to be returned by rand. These sequences are repeatable by calling srand with the same seed value.

If no seed value is provided, the functions are automatically seeded with a value of 1.

The


Examples

#include <stdlib.h>
#include <stdio.h>
 
int main( void )
{
  int randArray[20];
  int i=0;
    
  while(i<20)
  {
        randArray[i]=rand();
        printf("\n%d", randArray[i]);
        i++;
  }
 
  return 0;
}

         

Output

16807
282475249
1622650073
984943658
1144108930
470211272
101027544
1457850878
1458777923
2007237709
823564440
1115438165
1784484492
74243042
114807987
1137522503
1441282327
16531729
823378840
143542612

         

#include <stdlib.h>
#include <stdio.h>
int main( void )
{
 int seedVal = 45454652;
 int randVal;
 srand(seedVal);
 randVal=rand();
 printf("Random Value returned is %d"), randVal);
}

         

Output

Random Value returned is 1599641479

         

Errors

No errors are defined.

Feedback

For additional information or queries on this page send feedback

© 2005-2007 Nokia

Top