如何在8086汇编中生成随机数? [英] How to generate a random number in 8086 assembly?
问题描述
我想知道是否存在使用汇编生成随机数的例程或指令 在8086. 任何帮助将不胜感激.
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.
推荐答案
最常见的方法是使用时间戳.在32位模式下,可以通过rdtsc
指令在16位模式下进行:通过使用BIOS中断1A的功能0.
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;
还有 Wiki
这篇关于如何在8086汇编中生成随机数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!