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

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /** @file
  2. *
  3. * Random number generation
  4. *
  5. */
  6. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  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. if ( ! rnd_seed )
  18. rnd_seed = 4; /* Chosen by fair dice roll */
  19. }
  20. /**
  21. * Generate a pseudo-random number between 0 and 2147483647L or 2147483562?
  22. *
  23. * @ret rand Pseudo-random number
  24. */
  25. long int random ( void ) {
  26. int32_t q;
  27. if ( ! rnd_seed ) /* Initialize linear congruential generator */
  28. srandom ( currticks() );
  29. /* simplified version of the LCG given in Bruce Schneier's
  30. "Applied Cryptography" */
  31. q = ( rnd_seed / 53668 );
  32. rnd_seed = ( 40014 * ( rnd_seed - 53668 * q ) - 12211 * q );
  33. if ( rnd_seed < 0 )
  34. rnd_seed += 2147483563L;
  35. return rnd_seed;
  36. }