随机函数

下面这段代码是自己写的一段程序段,主要功能是在一个确定的路径下面随机搜索n个项目.

protected void Button1_Click(object sender, EventArgs e)
    
{
        
try
        
{
            Label3.Visible 
= true;
            Label3.Text 
= "";
            TableRow tr;
            TableCell tc;
            Random autoRand 
= new Random();
            ArrayList arr 
= new ArrayList();
            
double percentage = Convert.ToInt32(TextBox3.Text);
            percentage 
= percentage / 100;

            
string name = TextBox1.Text;
            
string str = "D:\\XXX\\" + name + "\\";
            
int num = 0;

            DirectoryInfo dir 
= new DirectoryInfo(str);

            
foreach (FileSystemInfo fsi in dir.GetFileSystemInfos())
            
{
                filename 
= "";
                
if (fsi is FileInfo)
                
{
                    num
++;
                    filename 
= fsi.FullName;
                    arr.Add(filename);
                }

                
if (fsi is DirectoryInfo)
                
{
                    num
++;
                    filename 
= fsi.FullName;
                    arr.Add(filename);
                }

            }


            
for (double i = 1; i <=percentage * num; i += 1)
            
{
                
int sum =Convert.ToInt32(percentage * num);
                
foreach (FileSystemInfo fsi in dir.GetFileSystemInfos())
                
{
                    filename 
= arr[autoRand.Next(1, num)].ToString();
                    tr 
= new TableRow();
                    tc 
= new TableCell();
                    tc.Controls.Add(
new LiteralControl(filename.ToString()));
                    tr.Cells.Add(tc);

                    Table1.Rows.Add(tr);
                    
break;
                }

            }

        }

        
catch
        
{
            Label3.Text 
= "不存在该项目,请确定后重新搜索!";
        }

    }
posted on 2006-06-09 12:59  Edwin dong  阅读(516)  评论(2)    收藏  举报

<% Function googleColor(value, random) Dim colorArray colorArray = Split(value, ",") googleColor = colorArray(random Mod (UBound(colorArray) + 1)) End Function Function googleScreenRes() Dim screenRes, delimiter, resArray screenRes = Request.ServerVariables("HTTP_UA_PIXELS") delimiter = "x" If IsEmpty(screenRes) Then screenRes = Request.ServerVariables("HTTP_X_UP_DEVCAP_SCREENPIXELS") delimiter = "," End If resArray = Split(screenRes, delimiter, 2) If (UBound(resArray) + 1) = 2 Then googleScreenRes = "&u_w=" & resArray(0) & "&u_h=" & resArray(1) End If End Function Function googleDcmguid() Dim dcmguid dcmguid = Request.ServerVariables("HTTP_X_DCMGUID") If Not IsEmpty(dcmguid) Then googleDcmguid = "&dcmguid=" & dcmguid End If End Function Dim googleTime, googleDt, googleScheme, googleHost googleTime = DateDiff("s", "01/01/1970 00:00:00", Now()) googleDt = (1000 * googleTime) + Round(1000 * (Timer - Int(Timer))) googleScheme = "http://" If StrComp(Request.ServerVariables("HTTPS"), "on") = 0 Then googleScheme = "https://" googleHost = Server.URLEncode(googleScheme & Request.ServerVariables("HTTP_HOST")) Dim googleAdUrl, googleAdOutput googleAdUrl = "http://pagead2.googlesyndication.com/pagead/ads?" &_ "ad_type=text_image" &_ "&channel=" &_ "&client=ca-mb-pub-6539345765131754" &_ "&dt=" & googleDt &_ "&format=mobile_single" &_ "&host=" & googleHost &_ "&ip=" & Server.URLEncode(Request.ServerVariables("REMOTE_ADDR")) &_ "&markup=xhtml" &_ "&oe=utf8" &_ "&output=xhtml" &_ "&ref=" & Server.URLEncode(Request.ServerVariables("HTTP_REFERER")) &_ "&url=" & googleHost & Server.URLEncode(Request.ServerVariables("URL")) &_ "&useragent=" & Server.URLEncode(Request.ServerVariables("HTTP_USER_AGENT")) &_ googleScreenRes() &_ googleDcmguid() Set googleAdOutput = Server.CreateObject("MSXML2.ServerXMLHTTP") googleAdOutput.Open "GET", googleAdUrl, false googleAdOutput.Send Response.Write(googleAdOutput.responseText) %>