How to generate a random number in 8086 assembly?

后端 未结 3 1012
误落风尘
误落风尘 2021-01-15 23:01

I want to know if there a routine or an instruction to generate a random number using assembly on 8086. any help will be appreciated.

3条回答
  •  清歌不尽
    2021-01-15 23:36

    The most common way is to use the timestamp. In 32 bit mode it can be done by rdtsc instruction, in 16 bit mode: by using function 0 of BIOS interrupt 1A.

    Because it's a timestamp, avoid using it frequently (because of lack of proper dispersion), and use it as seed for an pseudo-random number generator. When you need just one random value, you can use the timestamp directly.

    Usually a simple pseudo-random number generator is enough:

    static int seed = now();
    seed = (seed * LARGE_PRIME1) % LARGE_PRIME2;
    

    And there is also wiki

提交回复
热议问题