Để sinh số ngẫu nhiên trong C#, dùng phương thức Next(minValue, maxValue). Tham số đặt giá trị min/max.
Next(100, 200);
Dùng dưới đối tượng Random():
Random rd = new Random(); int rand_num = rd.Next(100, 200);
Ví dụ:
Ví dụ
using System;
class Program {
static void Main() {
Random rd = new Random();
int rand_num = rd.Next(100, 200);
Console.WriteLine(rand_num);
}
}
Kết quả
182