3

我对组装真的很陌生,我正在尝试创建一个简单的程序。为此,我需要生成一个随机数。

有人知道我如何使用 FASM 编译器做到这一点吗?

4

4 回答 4

5

You could use a Linear-Congruential Algorithm. Its the most common psuedo-random number algorithm.

Basically, you have a seed value. And then once you start generating random numbers each number becomes the seed for the new request.

The numbers are generated by

x = (a * s + b) MOD m

Where m, a and b are picked for the algorithm. There are some popular sets of these values used. Its a lot easier if you make m a power of 2, especially 2^32 for 32 bit machines. Then the mod step is done automatically by the machine.

Check out the wikipedia, they have popular sets of a, b and M and a lot more information.

There are more complicated things can be done with seeds as well (setting the seed based on the current time, for instance)

于 2009-01-26T12:17:39.790 回答
2

我是 R250 的忠实粉丝,它的执行速度比 LCG 快得多。 http://www.ddj.com/184408549?pgno=7

显示我以前写回的旧汇编代码的速度显着提高。

于 2009-01-26T13:13:09.693 回答
1

Take a look at this Wikipedia page, pick an algorithm, and implement it.

Edit: Or you could take the easy route. Use your OS's C runtime and call their rand functions.

于 2009-01-26T12:07:04.083 回答
0

随机数

这是一个有点模棱两可的问题。

到目前为止,大多数海报可能都是正确的;他们正在解释如何生成随机数,这可能就是您所需要的。使用当前时间播种算法(您必须向操作系统询问,或从时钟芯片中读取)。这将为您提供足以用于游戏和其他简单用途的“随机”数字。

不要将这些“随机数”用于任何安全应用程序(加密、密钥生成等)。对于安全应用程序,您需要一个非常好的密码安全随机数生成器。写其中一个真的很难。(Netscape弄错了,因此 Netscape Navigator 的早期版本有一个易于破解的 HTTPS 实现;Debian 最近弄错了,导致大量易于破解的 SSH 和 HTTPS/SSL 密钥)。

于 2009-01-26T13:24:20.747 回答