Csharp/C Sharp by API/System/Random

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

Random.Next()

  
using System;
class CondOpApp {
    [STAThread]
    static void Main(string[] args) {
        Random rand = new Random();
        int a = 0, b = 0;
        for (int i = 0; i < 5; i++) {
            a = rand.Next() % 100;
            b = rand.Next() % 100;
            Console.WriteLine("a={0}, b={1}, so the winner is: {2}", a, b, a > b ? "a" : "b");
        }
    }
}


Random.NextDouble()

  
using System;
using System.Windows.Forms;
using System.Drawing;
public class Test {
  static void Main() {
      Random roller = new Random();
      double toHit = roller.NextDouble();
      Console.WriteLine(toHit);
  }
}


Random.Next(range)

  
using System;
public class RandomIntegers
{
   public static void Main( string[] args )
   {
      Random randomNumbers = new Random(); 
      int face; 
      for ( int counter = 1; counter <= 20; counter++ )
      {
         face = randomNumbers.Next( 1, 7 );
         Console.Write( "{0}  ", face );
         if ( counter % 5 == 0 )
            Console.WriteLine();
      } 
   }
}