閱讀英文

共用方式為


Random 類別

定義

表示虛擬亂數產生器,此演算法可產生符合特定隨機統計需求的數字序列。

public class Random
[System.Serializable]
public class Random
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class Random
繼承
Random
屬性

範例

下列範例會建立單一隨機數產生器,並呼叫其 NextBytesNextNextDouble 方法來產生不同範圍內的隨機數序列。

// 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

下列範例會產生隨機整數,用來做為索引,以從陣列擷取字串值。

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

備註

如需此 API 的詳細資訊,請參閱 Random 的補充 API 備註

給繼承者的注意事項

在 .NET Framework 2.0 和更新版本中,、 和 方法的行為Next()已變更,因此這些方法不一定呼叫 方法的衍生類別實作Sample()NextBytes(Byte[])Next(Int32, Int32) 因此,衍生自Random該目標 .NET Framework 2.0 和更新版本的類別也應該覆寫這三種方法。

給呼叫者的注意事項

類別中 Random 隨機數產生器的實作不保證在主要版本的 .NET 之間維持不變。 因此,您不應該假設相同的種子會在不同版本的 .NET 中產生相同的虛擬隨機序列。

建構函式

Random()

使用預設種子值初始化 Random 類別的新執行個體。

Random(Int32)

使用指定的種子值,初始化 Random 類別的新執行個體。

屬性

Shared

提供可從任何線程並行使用的線程安全 Random 實例。

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetItems<T>(ReadOnlySpan<T>, Int32)

從提供的一組選擇中,建立以隨機選擇專案填入的陣列。

GetItems<T>(ReadOnlySpan<T>, Span<T>)

使用從提供的一組選擇中隨機選擇的專案,填入指定範圍的專案。

GetItems<T>(T[], Int32)

從提供的一組選擇中,建立以隨機選擇專案填入的陣列。

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
Next()

傳回非負值的隨機整數。

Next(Int32)

傳回小於指定之最大值的非負值隨機整數。

Next(Int32, Int32)

傳回指定範圍內的隨機整數。

NextBytes(Byte[])

以亂數填入指定位元組陣列的元素。

NextBytes(Span<Byte>)

以亂數填入指定位元組範圍的元素。

NextDouble()

傳回大於或等於 0.0,且小於 1.0 的隨機浮點數。

NextInt64()

傳回非負值的隨機整數。

NextInt64(Int64)

傳回小於指定之最大值的非負值隨機整數。

NextInt64(Int64, Int64)

傳回指定範圍內的隨機整數。

NextSingle()

傳回大於或等於 0.0,且小於 1.0 的隨機浮點數。

Sample()

傳回 0.0 和 1.0 之間的隨機浮點數。

Shuffle<T>(Span<T>)

執行範圍的就地隨機顯示。

Shuffle<T>(T[])

執行陣列的就地隨機顯示。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於

產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0