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