using System; using System.Security.Cryptography; class MainClass { public static void Main() { byte[] number = new byte[32]; RandomNumberGenerator rng = RandomNumberGenerator.Create(); rng.GetBytes(number); Console.WriteLine(BitConverter.ToString(number)); } }
Author: coder
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 6000 times.
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 Frequency" ); // output headers Console.WriteLine( "1 {0} 2 {1} 3 {2} 4 {3} 5 {4} 6 {5}", frequency1, frequency2, frequency3, frequency4, frequency5, frequency6 ); } } [/csharp]
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(); } } } [/csharp]
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'); } } }
ProcessStartInfo:FileName,Arguments,WorkingDirectory,WindowStyle,ErrorDialog
using System; using System.Diagnostics; class MainClass { public static void Main() { ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = "notepad.exe"; startInfo.Arguments = "file.txt"; startInfo.WorkingDirectory = @"C:Temp"; startInfo.WindowStyle = ProcessWindowStyle.Maximized; startInfo.ErrorDialog = true; Process process; try { process = Process.Start(startInfo); Console.WriteLine("Waiting 30 seconds for process to finish."); if (process.WaitForExit(30000)) { Console.WriteLine("Process terminated."); } else { Console.WriteLine("Timed out waiting for process to end."); } } catch (Exception ex) { Console.WriteLine("Could not start process."); Console.WriteLine(ex); } } }
ProcessWindowStyle.Maximized
using System; using System.Diagnostics; class MainClass { public static void Main() { ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = "notepad.exe"; startInfo.Arguments = "file.txt"; startInfo.WorkingDirectory = @"C:Temp"; startInfo.WindowStyle = ProcessWindowStyle.Maximized; startInfo.ErrorDialog = true; Process process; try { process = Process.Start(startInfo); Console.WriteLine("Waiting 30 seconds for process to finish."); if (process.WaitForExit(30000)) { Console.WriteLine("Process terminated."); } else { Console.WriteLine("Timed out waiting for process to end."); } } catch (Exception ex) { Console.WriteLine("Could not start process."); Console.WriteLine(ex); } } }