Csharp/CSharp Tutorial/Development/Random

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Demonstration of predictability (always prints the same thing)

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;

public class MainClass
{
    public static void Main()
    {
        Random r2 = new Random(99830123);
        Console.WriteLine(r2.Next());
        Console.WriteLine(r2.Next(150000));
        Console.WriteLine(r2.Next(9999, 100750));
        Console.WriteLine(r2.NextDouble());
    }
}
214873035
75274
85587
0.571986661558965

Generate random number

using System;  
  
class MainClass {     
  public static void Main() {     
    Random ran = new Random(); 
 
    Console.WriteLine(ran.Next(1, 7));  
  }     
}
6