1. Craig Phillips
  2. CodeSnippets

Commits

Craig Phillips  committed cc2da38 Merge

Merge

  • Participants
  • Parent commits 8e485a9, b3a65d6
  • Branches default

Comments (0)

Files changed (2)

File NonRepeatingRandom.cs

View file
  • Ignore whitespace
+using System;
+using System.Collections.Generic;
+
+namespace MiscellaneousClasses
+{
+	public class NonRepeatingRandom
+	{
+		private readonly List<Tuple<int, int>> randValues = new List<Tuple<int, int>>();
+		private int usageCounter = 0;
+		private readonly int period = 0;
+
+		public NonRepeatingRandom(int minValue, int maxValue)
+		{
+			if (maxValue <= minValue)
+			{
+				throw new ArgumentException("MaxValue must be greater than MinValue");
+			}
+
+			var rand = new Random();
+			period = maxValue - minValue;
+
+			for (var i = minValue; i <= maxValue; i++)
+			{
+				randValues.Add(new Tuple<int, int>(i, rand.Next()));
+			}
+			randValues.Sort((x, y) => y.Item2.CompareTo(x.Item2));
+		}
+
+		public int Next()
+		{
+			if (usageCounter > period)
+			{
+				usageCounter = 0;
+			}
+			var result = randValues[usageCounter];
+			usageCounter++;
+			return result.Item1;
+		}
+	}
+}

File RandomString.cs

View file
  • Ignore whitespace
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace MiscellaneousClasses
+{
+	public class RandomString
+	{
+		private readonly Random random = new Random();
+		private const string alphanumeric_selection = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
+		private const string symbol_selection = "!£$%^&*()-+=";
+
+		public string GenerateRandomString()
+		{
+			var length = random.Next(5, 15);
+			return GenerateRandomString(length);
+		}
+
+		public string GenerateRandomString(int length, bool onlyUseAlphaNumerics = false)
+		{
+			var characterSelection = onlyUseAlphaNumerics ? alphanumeric_selection : alphanumeric_selection + symbol_selection;
+			return GenerateRandomString(length, characterSelection);
+		}
+
+		public string GenerateRandomString(int length, string characterSelection)
+		{
+			var result = new StringBuilder();
+			for (var i = 0; i < length; i++)
+			{
+				var chr = characterSelection.Substring(random.Next(0, characterSelection.Length), 1);
+				result.Append(chr);
+			}
+			return result.ToString();
+		}
+
+		public string GenerateHashString(string stringToHash)
+		{
+			return BitConverter.ToString(new System.Security.Cryptography.SHA1CryptoServiceProvider().ComputeHash(Encoding.Unicode.GetBytes(stringToHash))).Replace("-", string.Empty);
+		}
+
+		public string GenerateRandomHashString()
+		{
+			return GenerateHashString(GenerateRandomString(1024));
+		}
+	}
+}