如何在8086汇编中生成随机数?

发布于 2024-10-06 06:37:21 字数 50 浏览 5 评论 0原文

我想知道是否有使用汇编生成随机数的例程或指令 在 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

jJeQQOZ5 2024-10-13 06:37:21

最常见的方法是使用时间戳。在32位模式下可以通过rdtsc指令来完成,在16位模式下可以通过使用BIOS中断1A的功能0来完成。

因为它是时间戳,所以避免频繁使用它(因为缺乏适当的分散),并将其用作伪随机数生成器的种子。当您只需要一个随机值时,可以直接使用时间戳。

通常一个简单的伪随机数生成器就足够了:

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

还有 wiki

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

金兰素衣 2024-10-13 06:37:21

对于这个问题,雅虎上有一个很好的答案!答案:

我怀疑这里的重点是学习
编写汇编语言。这是
指令集
(http://www.emu8086.com/assembler_tutorial/8086_instruction_set.html)
您拥有所需的所有说明
执行您需要的方程
生成伪随机数,除了
“mod”指令,您将
必须为其编写一个子程序。支付
特别注意
MUL 和 DIV 的限制
指示。 “存储在单独的
文件”不是隐含的
指令集。你必须要更好
了解这里想要什么。 “文件”
和“打印”是相关的概念
操作系统;它们的形式是
在这种情况下输出。

来自 http://answers.yahoo.com/question/index?qid= 20081030112909AAmjEsp

There is a good answer to this exact question on Yahoo! Answers:

I suspect the point here is to learn
to write assembly language. Here's the
instruction set
(http://www.emu8086.com/assembler_tutorial/8086_instruction_set.html)
You have all the instructions you need
to perform the equation you need to
generate pseudo-random numbers except
the 'mod' instruction, which you'll
have to write a subroutine for. Pay
particular attention to the
limitations on the MUL and DIV
instructions. "store in a separate
file" isn't implicit in the
instruction set. You'll have to better
understand what's wanted here. "File"
and "Print" are concepts related to
operating system; they're forms of
output in this case.

from http://answers.yahoo.com/question/index?qid=20081030112909AAmjEsp

牵你手 2024-10-13 06:37:21

您可能对 Agner Fog 的伪随机数生成器(其中一些是用汇编语言编写的)感兴趣,请访问此处

You might be interested in Agner Fog's pseudo random number generators (some of which are written in assembly) here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文