Now we need to randomly generate a group of RGB values, such as: # ffffff
The main point is to randomly generate 6 characters, these 6 characters from 0-9, take in the A-F. Then connect with # to form the color value. Public static string createrandomcolor ()
{
String allchar = "0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F ";
String [] allchararray = allchar. Split (',');
String randomcode = "";
Int temp =-1; random Rand = new random ();
For (INT I = 0; I <6; I ++)
{
If (temp! =-1)
{
Rand = new random (I * temp * (INT) datetime. Now. ticks ));
}
Int T = Rand. Next (15 );
If (temp = T)
{
Return createrandomcolor ();
}
Temp = T;
Randomcode + = allchararray [T];
}
Return "#" + randomcode;
} Debugging: Correct. Loop for 10 times (INT I = 0; I <= 10; I ++)
{
Console. writeline (createrandomcolor ());
. What's going on?
[Stathread]
Static Void Main ( String [] ARGs)
{
//
// Todo: addCodeTo start the applicationProgram
//
String Str1 = Createrandomcolor ();
String Str2;
For ( Int I = 0 ; I <= 10 ; I ++ )
{
Str2 = Createrandomcolor ();
If (Str2 ! = Str1)
{
Console. writeline (str2 );
Str2 = Str1;
}
}
}
Public Static String Createrandomcolor ()
{
String Allchar = " 0, 1, 2, 3, 4, 5, 6, 8, 9, A, B, C, D, E, F " ;
String [] Allchararray = Allchar. Split ( ' , ' );
String Randomcode = "" ;
Int Temp = - 1 ;
Random Rand = New Random ();
For ( Int I = 0 ; I < 6 ; I ++ )
{
If (Temp ! = - 1 )
{
Rand = New Random (I * Temp * (( Int ) Datetime. Now. ticks ));
}
Int T = Rand. Next ( 15 );
If (Temp = T)
{
Return Createrandomcolor ();
}
Temp = T;
Randomcode + = Allchararray [T];
}
Return " # " + Randomcode;
}