随机数

PowerShell: Get-Random System.Random

>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

JavaScript: Math.random()

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("/"))

Python: random 模块

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.

Functions :
Functions for integers
random.randrange(start, stop[, step])Return a randomly selected element from range(start, stop, step). This is equivalent to choice(range(start, stop, step)), but doesn’t actually build a range object.
random.randint(a, b)Return a random integer N such that a <= N <= b. Alias for randrange(a, b+1).
Functions for sequences
random.choice(seq)Return a random element from the non-empty sequence seq.
random.shuffle(x[, random]):Shuffle the sequence x in place.
random.sample(population, k):Return a k length list of unique elements chosen from the population sequence or set.

#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()。