Pseudorandom generator in Assembly Language

前端 未结 9 1026
予麋鹿
予麋鹿 2021-01-05 11:07

I need a pseudorandom number generator algorithm for a assembler program assigned in a course, and I would prefer a simple algorithm. However, I cannot use an external libra

9条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-05 11:38

    @jjrv
    What you're describing is actually a linear congrential generator. The most random bits are the highest bits. To get a number from 0..N-1 you multiply the full value by N (32 bits by 32 bits giving 64 bits) and use the high 32 bits.

    You shouldn't just use any number for a (the multiplier for progressing from one full value to the next), the numbers recommended in Knuth (Table 1 section 3.3.4 TAOCP vol 2 1981) are 1812433253, 1566083941, 69069 and 1664525.

    You can just pick any odd number for b. (the addition).

提交回复
热议问题