《操作系统》的实验代码。

26 lines
538 B

  1. #include <x86.h>
  2. #include <stdlib.h>
  3. static unsigned long long next = 1;
  4. /* *
  5. * rand - returns a pseudo-random integer
  6. *
  7. * The rand() function return a value in the range [0, RAND_MAX].
  8. * */
  9. int
  10. rand(void) {
  11. next = (next * 0x5DEECE66DLL + 0xBLL) & ((1LL << 48) - 1);
  12. unsigned long long result = (next >> 12);
  13. return (int)do_div(result, RAND_MAX + 1);
  14. }
  15. /* *
  16. * srand - seed the random number generator with the given number
  17. * @seed: the required seed number
  18. * */
  19. void
  20. srand(unsigned int seed) {
  21. next = seed;
  22. }