/// ------------------------------------------------------ /// RandomOps - (Pseudo) Random Number Generator For C# /// Copyright (C) 2003-2010 Magnus Erik Hvass Pedersen. /// Please see the file license.txt for license details. /// RandomOps on the internet: http://www.Hvass-Labs.org/ /// ------------------------------------------------------ using System.Diagnostics; namespace RandomOps { /// /// A set of integers enumerated from zero and upwards that /// can be drawn at random. Creation or resetting takes time /// O(n) where n is the size of the set, and drawing a random /// element from the set takes constant time O(1). Not thread-safe. /// public class Set { #region Public properties. /// /// Number of elements available for drawing. /// public int Size { get; private set; } /// /// Maximum number of elements in the set. /// public int Capacity { get; private set; } #endregion #region Constructor. /// /// Creates but does not initialize the random set. /// /// RNG object to use for random drawing. /// Number of elements in the set. public Set(Random rand, int capacity) { Rand = rand; Elms = new int[capacity]; Capacity = capacity; Size = 0; } #endregion #region Internal variables. /// /// The RNG used for drawing elements from the set. /// Random Rand; /// /// Array used for holding the elements of the set. /// int[] Elms; #endregion #region Internal methods. /// /// Swap the i'th and j'th elements of the internal array. /// void Swap(int i, int j) { Debug.Assert(i >= 0 && i < Capacity); Debug.Assert(j >= 0 && j < Capacity); int temp = Elms[i]; Elms[i] = Elms[j]; Elms[j] = temp; } /// /// Remove an element from the set. /// /// Position of element to be removed. void Remove(int index) { // Various assumptions. Debug.Assert(Size >= 1); Debug.Assert(index >= 0 && index < Size); // Decrease the number of elements in the set. Size--; // Swap element to be removed with the back of the array. Swap(index, Size); } #endregion #region Public methods. /// /// Draw and remove an element from the set. /// /// Randomly picked element. public int Draw() { // Local variables to be used. int index; int retVal; // Various assumptions. Debug.Assert(Size > 0); // Get random index from remainder of set. index = Rand.Index(Size); // Retrieve the element at that position. retVal = Elms[index]; // Remove that element from the set. Remove(index); return retVal; } /// /// Reset by filling the set with integer elements enumerated /// from zero and upwards. /// public void Reset() { // Reset index-variable. Size = Capacity; // Initialize array. for (int i = 0; i < Size; i++) { Elms[i] = i; } } /// /// Same as Reset() only that it is being followed by a removal /// of the designated element. /// /// Element to be removed after resetting. public void ResetExclude(int index) { Debug.Assert(index >= 0 && index < Capacity); Reset(); Remove(index); } #endregion } }