# 生成随机的，唯一的值C＃

``public Random a = new Random(DateTime.Now.Ticks.GetHashCode()); private void NewNumber() { MyNumber = a.Next(0, 10); }` `

`Random.Next`不保证数字是唯一的。 此外，您的范围是从0到10，您可能会得到重复值。 可能是你可以设置一个`int`列表并在列表中插入随机数字后检查它是否不包含重复。 就像是：

` `public Random a = new Random(); // replace from new Random(DateTime.Now.Ticks.GetHashCode()); // Since similar code is done in default constructor internally public List<int> randomList = new List<int>(); int MyNumber = 0; private void NewNumber() { MyNumber = a.Next(0, 10); if (!randomList.Contains(MyNumber)) randomList.Add(MyNumber); }` `

` `var nums = Enumerable.Range(0, 10).ToArray(); var rnd = new Random(); // Shuffle the array for (int i = 0;i < nums.Length;++i) { int randomIndex = rnd.Next(nums.Length); int temp = nums[randomIndex]; nums[randomIndex] = nums[i]; nums[i] = temp; } // Now your array is randomized and you can simply print them in order for (int i = 0;i < nums.Length;++i) Console.WriteLine(nums[i]);` `

` `using System; using System.Collections.Generic; namespace ConsoleApplication2 { static class Program { static void Main(string[] args) { Shuffler shuffler = new Shuffler(); List<int> list = new List<int>{ 1, 2, 3, 4, 5, 6, 7, 8, 9 }; shuffler.Shuffle(list); foreach (int value in list) { Console.WriteLine(value); } } } /// <summary>Used to shuffle collections.</summary> public class Shuffler { /// <summary>Creates the shuffler with a <see cref="MersenneTwister"/> as the random number generator.</summary> public Shuffler() { _rng = new Random(); } /// <summary>Shuffles the specified array.</summary> /// <typeparam name="T">The type of the array elements.</typeparam> /// <param name="array">The array to shuffle.</param> public void Shuffle<T>(IList<T> array) { for (int n = array.Count; n > 1; ) { int k = _rng.Next(n); --n; T temp = array[n]; array[n] = array[k]; array[k] = temp; } } private System.Random _rng; } }` `

` `//This code generates numbers between 1 - 100 and then takes 10 of them. var result = Enumerable.Range(1,101).OrderBy(g => Guid.NewGuid()).Take(10).ToArray();` `

` `using System; using System.Collections.Generic; using System.Linq; namespace SO14473321 { class Program { static void Main() { UniqueRandom u = new UniqueRandom(Enumerable.Range(1,10)); for (int i = 0; i < 10; i++) { Console.Write("{0} ",u.Next()); } } } class UniqueRandom { private readonly List<int> _currentList; private readonly Random _random = new Random(); public UniqueRandom(IEnumerable<int> seed) { _currentList = new List<int>(seed); } public int Next() { if (_currentList.Count == 0) { throw new ApplicationException("No more numbers"); } int i = _random.Next(_currentList.Count); int result = _currentList[i]; _currentList.RemoveAt(i); return result; } } }` `

` `using System; using System.Collections.Generic; namespace ConsoleApplication1 { class RnDHash { static void Main() { HashSet<int> rndIndexes = new HashSet<int>(); Random rng = new Random(); int maxNumber; Console.Write("Please input Max number: "); maxNumber = int.Parse(Console.ReadLine()); int iter = 0; while (rndIndexes.Count != maxNumber) { int index = rng.Next(maxNumber); rndIndexes.Add(index); iter++; } Console.WriteLine("Random numbers were found in {0} iterations: ", iter); foreach (int num in rndIndexes) { Console.WriteLine(num); } Console.ReadKey(); } } }` `

` `Random ran = new Random(); int randomno = ran.Next(0,100);` `

` `private void NewNumber() { Random a = new Random(Guid.newGuid().GetHashCode()); MyNumber = a.Next(0, 10); }` `

`Guid` ： 基于此 ：表示全局唯一标识符（GUID）

`Guid.newGuid()`生成一个唯一的标识符，如`"936DA01F-9ABD-4d9d-80C7-02AF85C822A8"`