您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

random.c 795B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /** @file
  2. *
  3. * Random number generation
  4. *
  5. */
  6. FILE_LICENCE ( GPL2_OR_LATER );
  7. #include <stdlib.h>
  8. #include <gpxe/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. }