Csharp/C Sharp/Development Class/Random — различия между версиями

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

Текущая версия на 11:43, 26 мая 2010

An automated pair of dice

/*
C#: The Complete Reference 
by Herbert Schildt 
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// An automated pair of dice. 
  
using System;  
  
public class RandDice {     
  public static void Main() {     
    Random ran = new Random(); 
 
    Console.Write(ran.Next(1, 7) + " ");  
    Console.WriteLine(ran.Next(1, 7));  
  }     
}


Get next random number in double

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);
  }
}


Get Random number

 
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 Color and Rectangle

 
using System;
using System.Drawing;
using System.Windows.Forms;
   
class RandomRectangle: Form
{
     public static void Main()
     {
          Application.Run(new RandomRectangle());
     }
     public RandomRectangle()
     {
          Text = "Random Rectangle";
   
          Timer timer    = new Timer();
          timer.Interval = 1;
          timer.Tick    += new EventHandler(TimerOnTick);
          timer.Start();
     }
     void TimerOnTick(object obj, EventArgs ea)
     {
          Random rand = new Random();
   
          int x1 = rand.Next(ClientSize.Width);
          int x2 = rand.Next(ClientSize.Width);
          int y1 = rand.Next(ClientSize.Height);
          int y2 = rand.Next(ClientSize.Height);
   
          Color color = Color.FromArgb(rand.Next(256), 
                                       rand.Next(256), 
                                       rand.Next(256));
   
          Graphics grfx = CreateGraphics();
          grfx.FillRectangle(new SolidBrush(color), 
                             Math.Min(x1,  x2), Math.Min(y1,  y2),
                             Math.Abs(x2 - x1), Math.Abs(y2 - y1));
          grfx.Dispose();
     }
}


Roll a six-sided die imes.

using System;
public class RollDie
{
   public static void Main( string[] args )
   {
      Random randomNumbers = new Random(); 
      int frequency1 = 0; 
      int frequency2 = 0; 
      int frequency3 = 0; 
      int frequency4 = 0; 
      int frequency5 = 0; 
      int frequency6 = 0; 
      int face; 
      for ( int roll = 1; roll <= 6000; roll++ )
      {
         face = randomNumbers.Next( 1, 7 ); 
         switch ( face )
         {
            case 1:
               frequency1++; 
               break;
            case 2:
               frequency2++; 
               break;
            case 3:
               frequency3++; 
               break;
            case 4:
               frequency4++; 
               break;
            case 5:
               frequency5++; 
               break;
            case 6:
               frequency6++; 
               break;
         }  
      } 
      Console.WriteLine( "Face\tFrequency" ); // output headers
      Console.WriteLine( "1\t{0}\n2\t{1}\n3\t{2}\n4\t{3}\n5\t{4}\n6\t{5}",
         frequency1, frequency2, frequency3, frequency4,
         frequency5, frequency6 );
   }
}


Shifted and scaled random integers.

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();
      } 
   }
}