using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptography; namespace BallotDrawRandNumGen.WebApi { public class BallotDrawHelper { private static readonly object syncLock = new object(); public virtual int[] GenerateRandomPositions(int count) { if (count <= 0) throw new ArgumentOutOfRangeException("count"); lock (syncLock) { //get the set of numbers from 1 to {count} var positions = Enumerable.Range(1, count).ToList(); // create a list to hold the new numbers var perm = new List(); for (int i = 0; i < count; i++) { // find the random index for list int n = RandomNumberGenerator.GetInt32(positions.Count); // push the value corresponds to the random index to the result list perm.Add(positions[n]); // remove the selected number from the lot positions.RemoveAt(n); } return perm.ToArray(); } } } }