Random 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示伪随机数生成器,该生成器是一种算法,该算法生成满足特定随机性统计要求的数字序列。
public ref class Random
public class Random
[System.Serializable]
public class Random
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class Random
type Random = class
[<System.Serializable>]
type Random = class
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Random = class
Public Class Random
- 继承
-
Random
- 属性
示例
以下示例创建一个随机数生成器并调用其 NextBytes, Next以及 NextDouble 用于在不同范围内生成随机数序列的方法。
// Instantiate random number generator using system-supplied value as seed.
var rand = new Random();
// Generate and display 5 random byte (integer) values.
byte[] bytes = new byte[5];
rand.NextBytes(bytes);
Console.WriteLine("Five random byte values:");
foreach (byte byteValue in bytes)
Console.Write("{0, 5}", byteValue);
Console.WriteLine();
// Generate and display 5 random integers.
Console.WriteLine("Five random integer values:");
for (int ctr = 0; ctr <= 4; ctr++)
Console.Write("{0,15:N0}", rand.Next());
Console.WriteLine();
// Generate and display 5 random integers between 0 and 100.
Console.WriteLine("Five random integers between 0 and 100:");
for (int ctr = 0; ctr <= 4; ctr++)
Console.Write("{0,8:N0}", rand.Next(101));
Console.WriteLine();
// Generate and display 5 random integers from 50 to 100.
Console.WriteLine("Five random integers between 50 and 100:");
for (int ctr = 0; ctr <= 4; ctr++)
Console.Write("{0,8:N0}", rand.Next(50, 101));
Console.WriteLine();
// Generate and display 5 random floating point values from 0 to 1.
Console.WriteLine("Five Doubles.");
for (int ctr = 0; ctr <= 4; ctr++)
Console.Write("{0,8:N3}", rand.NextDouble());
Console.WriteLine();
// Generate and display 5 random floating point values from 0 to 5.
Console.WriteLine("Five Doubles between 0 and 5.");
for (int ctr = 0; ctr <= 4; ctr++)
Console.Write("{0,8:N3}", rand.NextDouble() * 5);
// The example displays output like the following:
// Five random byte values:
// 194 185 239 54 116
// Five random integer values:
// 507,353,531 1,509,532,693 2,125,074,958 1,409,512,757 652,767,128
// Five random integers between 0 and 100:
// 16 78 94 79 52
// Five random integers between 50 and 100:
// 56 66 96 60 65
// Five Doubles.
// 0.943 0.108 0.744 0.563 0.415
// Five Doubles between 0 and 5.
// 2.934 3.130 0.292 1.432 4.369
// Instantiate random number generator using system-supplied value as seed.
let rand = Random()
// Generate and display 5 random byte (integer) values.
let bytes = Array.zeroCreate 5
rand.NextBytes bytes
printfn "Five random byte values:"
for byte in bytes do
printf "%5i" byte
printfn ""
// Generate and display 5 random integers.
printfn "Five random integer values:"
for _ = 0 to 4 do
printf $"{rand.Next(),15:N0}"
printfn ""
// Generate and display 5 random integers between 0 and 100.
printfn "Five random integers between 0 and 100:"
for _ = 0 to 4 do
printf $"{rand.Next 101,8:N0}"
printfn ""
// Generate and display 5 random integers from 50 to 100.
printfn "Five random integers between 50 and 100:"
for _ = 0 to 4 do
printf $"{rand.Next(50, 101),8:N0}"
printfn ""
// Generate and display 5 random floating point values from 0 to 1.
printfn "Five Doubles."
for _ = 0 to 4 do
printf $"{rand.NextDouble(),8:N3}"
printfn ""
// Generate and display 5 random floating point values from 0 to 5.
printfn "Five Doubles between 0 and 5."
for _ = 0 to 4 do
printf $"{rand.NextDouble() * 5.0,8:N3}"
// The example displays output like the following:
// Five random byte values:
// 194 185 239 54 116
// Five random integer values:
// 507,353,531 1,509,532,693 2,125,074,958 1,409,512,757 652,767,128
// Five random integers between 0 and 100:
// 16 78 94 79 52
// Five random integers between 50 and 100:
// 56 66 96 60 65
// Five Doubles.
// 0.943 0.108 0.744 0.563 0.415
// Five Doubles between 0 and 5.
// 2.934 3.130 0.292 1.432 4.369
Module Example
Public Sub Main()
' Instantiate random number generator using system-supplied value as seed.
Dim rand As New Random()
' Generate and display 5 random byte (integer) values.
Dim bytes(4) As Byte
rand.NextBytes(bytes)
Console.WriteLine("Five random byte values:")
For Each byteValue As Byte In bytes
Console.Write("{0, 5}", byteValue)
Next
Console.WriteLine()
' Generate and display 5 random integers.
Console.WriteLine("Five random integer values:")
For ctr As Integer = 0 To 4
Console.Write("{0,15:N0}", rand.Next)
Next
Console.WriteLine()
' Generate and display 5 random integers between 0 and 100.'
Console.WriteLine("Five random integers between 0 and 100:")
For ctr As Integer = 0 To 4
Console.Write("{0,8:N0}", rand.Next(101))
Next
Console.WriteLine()
' Generate and display 5 random integers from 50 to 100.
Console.WriteLine("Five random integers between 50 and 100:")
For ctr As Integer = 0 To 4
Console.Write("{0,8:N0}", rand.Next(50, 101))
Next
Console.WriteLine()
' Generate and display 5 random floating point values from 0 to 1.
Console.WriteLine("Five Doubles.")
For ctr As Integer = 0 To 4
Console.Write("{0,8:N3}", rand.NextDouble())
Next
Console.WriteLine()
' Generate and display 5 random floating point values from 0 to 5.
Console.WriteLine("Five Doubles between 0 and 5.")
For ctr As Integer = 0 To 4
Console.Write("{0,8:N3}", rand.NextDouble() * 5)
Next
End Sub
End Module
' The example displays output like the following:
' Five random byte values:
' 194 185 239 54 116
' Five random integer values:
' 507,353,531 1,509,532,693 2,125,074,958 1,409,512,757 652,767,128
' Five random integers between 0 and 100:
' 16 78 94 79 52
' Five random integers between 50 and 100:
' 56 66 96 60 65
' Five Doubles.
' 0.943 0.108 0.744 0.563 0.415
' Five Doubles between 0 and 5.
' 2.934 3.130 0.292 1.432 4.369
以下示例生成一个随机整数,该整数用作索引从数组中检索字符串值。
Random rnd = new();
string[] malePetNames = [ "Rufus", "Bear", "Dakota", "Fido",
"Vanya", "Samuel", "Koani", "Volodya",
"Prince", "Yiska" ];
string[] femalePetNames = [ "Maggie", "Penny", "Saya", "Princess",
"Abby", "Laila", "Sadie", "Olivia",
"Starlight", "Talla" ];
// Generate random indexes for pet names.
int mIndex = rnd.Next(malePetNames.Length);
int fIndex = rnd.Next(femalePetNames.Length);
// Display the result.
Console.WriteLine("Suggested pet name of the day: ");
Console.WriteLine($" For a male: {malePetNames[mIndex]}");
Console.WriteLine($" For a female: {femalePetNames[fIndex]}");
// The example displays output similar to the following:
// Suggested pet name of the day:
// For a male: Koani
// For a female: Maggie
let rnd = Random()
let malePetNames =
[| "Rufus"; "Bear"; "Dakota"; "Fido";
"Vanya"; "Samuel"; "Koani"; "Volodya";
"Prince"; "Yiska" |]
let femalePetNames =
[| "Maggie"; "Penny"; "Saya"; "Princess";
"Abby"; "Laila"; "Sadie"; "Olivia";
"Starlight"; "Talla" |]
// Generate random indexes for pet names.
let mIndex = rnd.Next malePetNames.Length
let fIndex = rnd.Next femalePetNames.Length
// Display the result.
printfn "Suggested pet name of the day: "
printfn " For a male: %s" malePetNames.[mIndex]
printfn " For a female: %s" femalePetNames.[fIndex]
// The example displays output similar to the following:
// Suggested pet name of the day:
// For a male: Koani
// For a female: Maggie
Module Example
Public Sub Main()
Dim rnd As New Random()
Dim malePetNames() As String = { "Rufus", "Bear", "Dakota", "Fido",
"Vanya", "Samuel", "Koani", "Volodya",
"Prince", "Yiska" }
Dim femalePetNames() As String = { "Maggie", "Penny", "Saya", "Princess",
"Abby", "Laila", "Sadie", "Olivia",
"Starlight", "Talla" }
' Generate random indexes for pet names.
Dim mIndex As Integer = rnd.Next(malePetNames.Length)
Dim fIndex As Integer = rnd.Next(femalePetNames.Length)
' Display the result.
Console.WriteLine("Suggested pet name of the day: ")
Console.WriteLine(" For a male: {0}", malePetNames(mIndex))
Console.WriteLine(" For a female: {0}", femalePetNames(fIndex))
End Sub
End Module
' The example displays output similar to the following:
' Suggested pet name of the day:
' For a male: Koani
' For a female: Maggie
注解
注释
若要生成加密安全的随机数(例如适合创建随机密码的随机数字),请使用类中的 RandomNumberGenerator 一种静态方法。
有关此 API 的详细信息,请参阅 Random 的补充 API 备注。
继承者说明
在 .NET Framework 2.0 及更高版本中,和方法的行为Next()Next(Int32, Int32)已更改,NextBytes(Byte[])因此这些方法不一定调用方法的Sample()派生类实现。 因此,派生自 Random 该目标 .NET Framework 2.0 及更高版本的类还应重写这三种方法。
调用方说明
无法保证类中 Random 随机数生成器的实现在 .NET 的主要版本中保持不变。 因此,不应假定同一种子会在不同版本的 .NET 中生成相同的伪随机序列。
构造函数
| 名称 | 说明 |
|---|---|
| Random() |
使用默认种子值初始化类的新实例 Random 。 |
| Random(Int32) |
使用指定的种子值初始化类的新实例 Random 。 |
属性
| 名称 | 说明 |
|---|---|
| Shared |
提供可从任何线程并发使用的线程安全 Random 实例。 |
方法
| 名称 | 说明 |
|---|---|
| Equals(Object) |
确定指定的对象是否等于当前对象。 (继承自 Object) |
| GetHashCode() |
用作默认哈希函数。 (继承自 Object) |
| GetHexString(Int32, Boolean) |
创建一个用随机十六进制字符填充的字符串。 |
| GetHexString(Span<Char>, Boolean) |
使用随机十六进制字符填充缓冲区。 |
| GetItems<T>(ReadOnlySpan<T>, Int32) |
创建一个数组,其中包含从提供的一组选项中随机选择的项。 |
| GetItems<T>(ReadOnlySpan<T>, Span<T>) |
用从提供的一组选项中随机选择的项填充指定范围的元素。 |
| GetItems<T>(T[], Int32) |
创建一个数组,其中包含从提供的一组选项中随机选择的项。 |
| GetString(ReadOnlySpan<Char>, Int32) |
创建一个用随机选择 |
| GetType() |
获取当前实例的 Type。 (继承自 Object) |
| MemberwiseClone() |
创建当前 Object的浅表副本。 (继承自 Object) |
| Next() |
返回非负随机整数。 |
| Next(Int32, Int32) |
返回指定范围内随机整数。 |
| Next(Int32) |
返回小于指定最大值的非负随机整数。 |
| NextBytes(Byte[]) |
用随机数填充指定字节数组的元素。 |
| NextBytes(Span<Byte>) |
用随机数填充指定字节范围的元素。 |
| NextDouble() |
返回一个大于或等于 0.0 且小于 1.0 的随机浮点数。 |
| NextInt64() |
返回非负随机整数。 |
| NextInt64(Int64, Int64) |
返回指定范围内随机整数。 |
| NextInt64(Int64) |
返回小于指定最大值的非负随机整数。 |
| NextSingle() |
返回一个大于或等于 0.0 且小于 1.0 的随机浮点数。 |
| Sample() |
返回介于 0.0 和 1.0 之间的随机浮点数。 |
| Shuffle<T>(Span<T>) |
执行跨度的就地混排。 |
| Shuffle<T>(T[]) |
执行数组的就地随机排列。 |
| ToString() |
返回一个表示当前对象的字符串。 (继承自 Object) |