Using lock for random

This commit is contained in:
Jaex 2013-11-20 23:36:00 +02:00
parent 41777a7a41
commit 79f0c09546
2 changed files with 18 additions and 4 deletions

View file

@ -295,8 +295,7 @@ public static Color CMYKToColor(CMYK cmyk)
public static Color RandomColor()
{
Random rand = Helpers.Random;
return Color.FromArgb(rand.Next(256), rand.Next(256), rand.Next(256));
return Color.FromArgb(Helpers.Random(255), Helpers.Random(255), Helpers.Random(255));
}
public static Color ParseColor(string color)

View file

@ -54,9 +54,24 @@ public static class Helpers
public const string URLPathCharacters = URLCharacters + "/"; // 47
public const string ValidURLCharacters = URLPathCharacters + ":?#[]@!$&'()*+,;=";
public static readonly Random Random = new Random();
private static readonly object randomLock = new object();
private static readonly Random random = new Random();
public static readonly Version OSVersion = Environment.OSVersion.Version;
public static int Random(int max)
{
return Random(0, max);
}
public static int Random(int min, int max)
{
lock (randomLock)
{
return random.Next(min, max + 1);
}
}
public static string GetFilenameExtension(string filePath)
{
if (!string.IsNullOrEmpty(filePath) && filePath.Contains('.'))
@ -131,7 +146,7 @@ public static string HourTo12(int hour)
public static char GetRandomChar(string chars)
{
return chars[Random.Next(chars.Length)];
return chars[Random(chars.Length - 1)];
}
public static string GetRandomString(string chars, int length)