随机数

记得上大学时,曾经做过这样一个VB题。大概意思入下:Winform上有一个按钮,当鼠标移到上面时,按钮在屏幕的范围的移动。按钮的Text是"你抓不到我"。
我当时做的时候用的MouseOver事件,在事件里得到Mouse的x.y坐标。然后生成一个随机数。更改按钮的x。y实现移动。  当日自己犯了一个错误使按钮基本不动。知道是什么错误吗?

那就是Randomize()函数没有调用


 

随着Microsoft产品的工业化生产。他们出来的软件越来越完善化、傻瓜化。

在C#不需要Randomize了【VB.Net不知道怎么样?师弟师妹可以试试】

现在有个题

一个长度为10000的字符串,通过随机从a-z中抽取10000个字符组成。请用c#语言编写主要程序来实现

 

方法1
        
int a = Convert.ToInt32('a');
        
int z = Convert.ToInt32('z');
        StringBuilder sb 
= new StringBuilder();
        System.Random random 
= new Random(Environment.TickCount);
        
for (int i = 1; i <= 10000; i++)
        
{

            
int c = random.Next(a, z);
            sb.Append(Convert.ToChar(c));
        }

        Console.WriteLine(sb.ToString());
        Console.Read();


 

方法二

 

        int min = System.Convert.ToInt32('a');
        
int max = System.Convert.ToInt32('z');
        
//create a byte array to hold random value
        byte[] bytes = new byte[1];
        
//create a instance of the RNGCryptoServiceProvider
        System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
        
//fill the random data with bytes


        System.Text.StringBuilder sb 
= new System.Text.StringBuilder();

        
for (int i = 1; i <= 10000; i++)
        
{
            rng.GetBytes(bytes);
            
//min=<((x/255)*(max-min))+min<=max
            int number = (int)(((decimal)bytes[0/ 255* (max - min)) + min;
            sb.Append(System.Convert.ToChar(number));

        }

        System.Console.WriteLine(sb.ToString());
        System.Console.Read();
posted @ 2008-06-25 17:49  roboth  阅读(444)  评论(0)    收藏  举报