Random
产生浮点型伪随机数.
Random ( [Min = 0 [, Max = 1 [, Flag = 0]]] )
参数
Min
|
[可选] 随机数的最小值,默认为 0.
|
Max
|
[可选] 随机数的最大值,默认为 1.
|
Flag
|
[可选] 设为 1, 则返回整数,默认返回浮点数.
|
返回值
成功:
|
返回最小值与最大值之间的伪随机数.
|
失败:
|
@error 设置 为非 0 值.
|
备注
如果只提供一个参数, 则它解释为最大值.
其结果在取整数时, 返回值在最小值及最大值之间, 包含最小, 最大值(取浮点数时可以缺少"最大值"参数).
如果最小和最大值相同, 则本函数将返回那个值 - 这种情况下 @error 标志不会被设置
当使用整数时, 最大-最小必须小于 2^31.
关于源型的说明
函数使用 Mersenne Twister 的 MT19937 随机数发生器,
由 Takuji Nishimura, Makoto Matsumoto, Shawn Cokus, Matthe Bellew 与 Isaku Wada 编写.
Mersenne Twister 算法用于生成随机数. 设计考虑了其它各种发生器的缺点.
周期为 219937-1, 而且在 623 维空间上的分布是均匀的.
产生随机数的速度也挺快; 它避免了乘法和除法的使用, 同时还很好地利用了缓冲和管道.
详细说明请查看算法作者的网页 http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
版权 (C) 1997 - 2002, Makoto Matsumoto 与 Takuji Nishimura 保留所有版权.
如果满足以下条件, 则允许以源代码格式或二进制格式, 经过修改或不经修改, 进行再分发和使用:
1. 再分发源代码时,必须保留上述版权说明,此条件列表和以下免责声明.
2. 以二进制格式再分发必须在文档和/或随分发版本一起提供的其他资料中复制上述版权说明, 此条件列表和以下免责声明.
3. 没有专门的事先书面许可,不得用贡献者的姓名来签署或宣传由此软件衍生的产品.
此软件由版权拥有者和贡献者"照原样"提供. 对于任何明示或暗示的担保, 包括但不限于对商业可行性, 针对特定用途的适用性的暗示担保, 我们均不负责.
对于任何直接,间接,偶然,特别,典型或因果性损坏(包括但不限于替代商品或服务的获得; 用法, 数据或利润的丢失;或业务中断), 无论起因是什么, 无论根据任何责任理论, 无论是否在合同中有严格赔偿责任, 版权所有者及其贡献者均一概不负责.
对于由于使用此软件, 而以任何形式出现的民事侵权行为(包括疏忽或其他形式), 版权所有者及其贡献者均一概不负责. 即使被告知了这种损坏的可能性,也是如此.
函数示例
示例 1
#include <MsgBoxConstants.au3>
; Flip a coin.
Example()
Func Example()
If Random(0, 1, 1) Then ; Return an integer between 0 and 1.
MsgBox($MB_SYSTEMMODAL, "", "The side of the coin was: Heads") ; If the random integer was 1 then heads was thrown.
Else
MsgBox($MB_SYSTEMMODAL, "", "The side of the coin was: Tails") ; If the random integer was 0 then tails was thrown.
EndIf
EndFunc ;==>Example
示例 2
#include <MsgBoxConstants.au3>
; Roll a die.
Example()
Func Example()
MsgBox($MB_SYSTEMMODAL, "", "The die landed on number " & Random(1, 6, 1) & ".") ; Return an integer between 1 and 6.
EndFunc ;==>Example
示例 3
#include <MsgBoxConstants.au3>
; Create a random string of text.
Example()
Func Example()
Local $sText = ""
For $i = 1 To Random(5, 20, 1) ; Return an integer between 5 and 20 to determine the length of the string.
$sText &= Chr(Random(65, 122, 1)) ; Return an integer between 65 and 122 which represent the ASCII characters between a (lower-case) to Z (upper-case).
Next
MsgBox($MB_SYSTEMMODAL, "", "The random string of text was: " & $sText) ; Display the string of text.
EndFunc ;==>Example
示例 4
#include <MsgBoxConstants.au3>
; Result of when Min and Max are the same value.
Example()
Func Example()
Local $iRandom = Random(10, 10)
If @error Then
MsgBox($MB_SYSTEMMODAL, "", "An error occurred, due to the fact both values are exactly the same: " & $iRandom)
EndIf
EndFunc ;==>Example
----------------------------------------
该函数可以通过命令 exect 调用
Random(0,1,1) ;; 返回一个值0或1
Random(-10,10,1) ;; 产生-10和10之间的整数
Chr(Random(Asc('A'),Asc('Z'),1)) ;; 随机字母
exect=$var_ss=Chr(Random(Asc('A'),Asc('Z'),1)) GLOBALEXECT<a> ;; 随机字母
© Аверин Андрей для Total Commander Image Averin-And@yandex.ru
|