若在concurrency中, 有時須要產生一堆random number去做, 但工作上, 要制做一條unique path, 除了用primary key 外, timestamp + random number 亦是一個好選擇. 為了更精準 (或者無聊), 便嘗試了random string.
private string RandomString(int size)
{
Random random = new Random((int)DateTime.Now.Ticks);
StringBuilder builder = new StringBuilder();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
builder.Append(ch);
}
return builder.ToString();
}
用法:
RandomString(attachmentCachePathRandomStringSize)
Reference:
http://stackoverflow.com/questions/1122483/random-string-generator-returning-same-string
Leave a Reply