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.

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