2012年3月26日星期一

Random伪随机数

Random伪随机数



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace 伪随机数
{
class Program
{
static void Main(string[] args)
{
Random rnd=new Random ();
char c;
Random ro = new Random();
Console.WriteLine("{0}", ro);


int d = ro.Next();
Console.WriteLine("{0}",d);

Random rc = new Random();
int f = rc.Next();
Console.WriteLine("{0}",f);

//Thread.Sleep(10000);
//Console.WriteLine("time later 10000");


Random s = new Random();
double cc = s.NextDouble();
Console.WriteLine("{0}",cc);


int count = 4;
Random[] rnds = new Random[count];
int[] ss = new int[4];
for (int i = 0; i < count; i++)
{
rnds[i] = new Random(unchecked((int)(DateTime.Now.Ticks >> i)));
ss[i] = rnds[i].Next();
Console.WriteLine("{0}",ss[i]);
}

Random ww = new Random();
double ee = ww.NextDouble();
string gg = string.Format("{0:F5}", ee);
Console.WriteLine("{0}",gg);

int k = rc.Next(65, 91);
c = (char)k;
Console.WriteLine("{0}", c);

string rr;
rr = GetLetter(rnd);
Console.WriteLine("{0}",rr);
Console.ReadKey();

}
public static string GetLetter(Random rnd)
{
// A-Z ASCII值 65-90
// a-z ASCII值 97-122

int i = rnd.Next(65, 123);
char o = (char)i;
if (char.IsLetter(o))
{
return o.ToString();
}
else
{
// 递归调用,直到随机到字母
return GetLetter(rnd);
}
}
}
}



TAG: