Random

AutoHotkey

Random

生成伪随机数。

Random, OutputVar [, Min, Max]
Random, , NewSeed

参数

OutputVar

用来存储结果的变量名. 存储的浮点数的格式由 SetFormat 决定.

Min
可以生成的最小数字, 可以为负数, 浮点数或 表达式. 如果省略, 最小的数字默认为 0. 允许的最小整数值为 -2147483648, 但浮点数没有限制.
Max

可以生成的最大数字, 可以为负数, 浮点数或 表达式. 如果省略, 最大的数字默认为 2147483647 (这也是允许的最大整数值, 但浮点数没有限制).

NewSeed

此模式使用 NewSeed 重新设定随机数生成器的种子 (可以为 表达式). 这会影响后续生成的所有随机数. NewSeed 应该为介于 0 和 4294967295 (0xFFFFFFFF) 之间的整数. 重新设定种子可以提高随机数的质量/安全性, 尤其当 NewSeed 是真正的随机数而不是质量不佳的伪随机数时. 重新设定种子通常只需要进行一次.

如果脚本没有重新设定种子, 则使用自 1601 年 1 月 1 日开始 100 纳秒间隔数目的 64 位值的低 32 位作为种子开始. 这个值在 ~7.2 分钟内从 0 变化到 4294967295.

备注

此命令生成伪随机数, 即一个模拟真正随机数的数字, 但它实际上是通过复杂的公式计算使得判断/猜测下一个数字极为困难的数字.

在指定范围中所有数字被生成的机率几乎是相同的 (然而, 请参阅下面的 "已知限制").

如果 MinMax 其中一个为小数,则最后结果将为由 SetFormat 设置格式的浮点数。否则,结果为整数。

结果为浮点数的已知限制: 1) 对于任何特殊的范围只能生成大约 4,294,967,296 个截然不同的数字, 所以永远不会生成范围中的其他所有数字; 2) 优势结果可能比指定的 Max 略大一些 (这种情况部分是由浮点数固有的不精确性导致的).

相关

SetFormat

示例

Random, rand, 1, 10
Random, rand, 0.0, 1.0

基于原始来源的注释

This function uses the Mersenne Twister random number generator, MT19937, written by Takuji Nishimura and Makoto Matsumoto, Shawn Cokus, Matthe Bellew and Isaku Wada.

The Mersenne Twister is an algorithm for generating random numbers. It was designed with consideration of the flaws in various other generators. The period, 219937-1, and the order of equidistribution, 623 dimensions, are far greater. The generator is also fast; it avoids multiplication and division, and it benefits from caches and pipelines. For more information see the inventors' web page at www.math.keio.ac.jp/~matumoto/emt.html

Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  3. The names of its contributors may not be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Do NOT use for CRYPTOGRAPHY without securely hashing several returned values together, otherwise the generator state can be learned after reading 624 consecutive values.

When you use this, send an email to: [email protected] with an appropriate reference to your work. It would be nice to CC: [email protected] and [email protected] when you write.

This above has been already been done for AutoHotkey, but if you use the Random command in a publicly distributed application, consider sending an e-mail to the above people to thank them.