>Get-Random This command gets a random integer between 0 (zero) and Int32.MaxValue.
>Get-Random -Maximum 100 This command gets a random integer between 0 (zero) and 99.
>Get-Random -Minimum 10.7 -Maximum 20.93 This command gets a random floating-point number greater than or equal to 10.7 and less than 20.92.
>Get-Random -InputObject 1, 2, 3, 5, 8, 13 -Count 3 This command gets three randomly selected numbers in random order from the array.
请看下面的例子。
$al = New-Object System.Collections.ArrayList($null) #$al = @() #$al = [System.Collections.ArrayList]$al for($i =0; $i -lt 100; $i++){ $t = Get-Random -minimum 10 -maximum 100 $al.add($t); } $al
可以直接使用 .net 中的System.Random 类。
#定义ArrayList $array = New-Object System.Collections.ArrayList($null) #定义随机数对象 $rnd = New-Object System.Random($null) for($i = 0; $i -lt 100; $i++){ $t = $rnd.next(100,200) #利用Random对象的next函数产生一个范围的随机数。 $array.Add($t) } $array
Math.random():返回[0, 1)之间的小数。
返回[m, n) 之间的整数的公式: m + Math.floor(Math.random() * (n - m))
array = new Array() for(i = 0; i < 300; i++){ array.push(Math.floor(10 + Math.random() * 90)) //[10, 100) } console.log(array.join("/"))
This module implements pseudo-random number generators for various distributions.For integers, there is uniform selection from a range. For sequences, there is uniform selection of a random element, a function to generate a random permutation of a list in-place, and a function for random sampling without replacement.
#random 模块,处理随机数的相关操作 import random array = [] for x in range(100): array.append(random.randrange(1,100,7)) result= 0 for x in array: result += x print(array) print("Sum of array is:%d." % result)
PowerShell 由内置函数Get-Random生成随机数, $v = Get-Random -Maximum 100。
JavaScript 由JavaScript的Math对象的random()函数处理,,var value = m + Math.random() * (n - m)
Python 由random 模块处理,, v = random.randint(m, n),因为返回的是dict数据类型。
JavaScript 和 Python 都由外置模块的函数提供随机数功能,PowerShell 应该也可以使用.net 的随机函数,System.Random()。