在 C# 中生成隨機數
若要生成隨機數,請使用 Random 類。
建立一個物件 −
Random r = new Random();
現在,使用 Next() 方法在範圍之間獲取隨機數 −
r.Next(10,50);
以下是完整程式碼 −
示例
using System; public class Program { public static void Main() { Random r = new Random(); int genRand= r.Next(10,50); Console.WriteLine("Random Number = "+genRand); } }
輸出
Random Number = 24
廣告