You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

random.c 738B

1234567891011121314151617181920212223242526272829303132333435363738
  1. /** @file
  2. *
  3. * Random number generation
  4. *
  5. */
  6. #include <stdlib.h>
  7. static int32_t rnd_seed = 0;
  8. /**
  9. * Seed the pseudo-random number generator
  10. *
  11. * @v seed Seed value
  12. */
  13. void srandom ( unsigned int seed ) {
  14. rnd_seed = seed;
  15. }
  16. /**
  17. * Generate a pseudo-random number between 0 and 2147483647L or 2147483562?
  18. *
  19. * @ret rand Pseudo-random number
  20. */
  21. long int random ( void ) {
  22. int32_t q;
  23. if ( ! rnd_seed ) /* Initialize linear congruential generator */
  24. srandom ( currticks() );
  25. /* simplified version of the LCG given in Bruce Schneier's
  26. "Applied Cryptography" */
  27. q = ( rnd_seed / 53668 );
  28. rnd_seed = ( 40014 * ( rnd_seed - 53668 * q ) - 12211 * q );
  29. if ( rnd_seed < 0 )
  30. rnd_seed += 2147483563L;
  31. return rnd_seed;
  32. }