[Import] custom randomization class. net

Source: Internet
Author: User
Tags random seed
The Random Seed is accurate to milliseconds in computer systems and may be repeated. This time I encountered this problem, and the computer would be too fast.

VB.net
Public class myrandom
Private _ strin as string
Private _ Strout as string
Private _ RND as random

Public sub new (byval strin as string)

_ RND = new random (system. environment. tickcount) 'ensures that different random numbers are obtained each time. system. environment. tickcount indicates the number of milliseconds since the system was started.
_ Strin = strin
End sub

Private function getonerandomnum (byval strformat as string) as string
Dim strtemp as string
Select case strformat
Case "*"
Dim itmp as integer = _ RND. Next (36)
If itmp <10 then
Strtemp = _ RND. Next (10). tostring ()
Else
Strtemp = convert. tochar (_ RND. Next (26) + 65). tostring ()
End if

Case "#"
Strtemp = _ RND. Next (10). tostring ()

Case "$"
Strtemp = convert. tochar (_ RND. Next (26) + 65). tostring ()

Case else
Strtemp = strformat

End select
Return strtemp

End Function

Public Function getrandomnum () as string
_ Strout = string. Empty
Dim I as integer
For I = 0 to _ strin. Length-1
_ Strout + = me. getonerandomnum (_ strin. substring (I, 1 ))
Next
Return _ Strout
End Function

End Class

C #

Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;

/// <Summary>
/// Summary description for myrandom
/// </Summary>
Public class myrandom
{
Private string _ strin;
Private string _ Strout;
Private random _ RND;

Public myrandom (string strin)
{

_ RND = new random (system. environment. tickcount); // ensure that different random numbers are obtained each time. system. environment. tickcount indicates the number of milliseconds since the system was started.
_ Strin = strin;

//
// Todo: Add constructor logic here
//
}
Private string getonerandomnum (string strformat)
{
String strtemp;
Switch (strformat)
{
Case "*":
{
Int itmp = _ RND. Next (36 );
If (itmp <10)
Strtemp = _ RND. Next (10). tostring ();
Else
Strtemp = convert. tochar (_ RND. Next (26) + 'A'). tostring ();
Break;
}
Case "#":
{
Strtemp = _ RND. Next (10). tostring ();
Break;
}
Case "$ ":
{
Strtemp = convert. tochar (_ RND. Next (26) + 'A'). tostring ();
Break;
}
Default:
{
Strtemp = strformat;
Break;
}
}
Return strtemp;

}

Public String getrandomnum ()
{
_ Strout = string. empty;
For (INT I = 0; I <_ strin. length; I ++)
{
_ Strout + = This. getonerandomnum (_ strin [I]. tostring ());
}
Return _ Strout;
}

}
Source: http://vlly.net/myblog/article.asp? Id = 2153

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.