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 762B

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