Generate random color values

Source: Internet
Author: User
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;
}

Related Article

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.