using System.Collections.Generic;
using System.Security.Cryptography;
public static void Main()
var ints = new List<int>{1, 4, 6, 7, 8, 11, 14, 19, 21, 20, 10};
var rand = new RandomGenerator().Next(1, int.MaxValue);
var query = ints.Where(a => a % 2 == 0);
Console.WriteLine("Rand: " + rand);
query = rand % 2 == 0 ? query.OrderBy(a => a) : query.OrderByDescending(a => a);
query.ToList().ForEach(a => Console.WriteLine(a));
public class RandomGenerator
readonly RNGCryptoServiceProvider csp;
csp = new RNGCryptoServiceProvider();
public int Next(int minValue, int maxExclusiveValue)
if (minValue >= maxExclusiveValue)
throw new ArgumentOutOfRangeException("minValue must be lower than maxExclusiveValue");
long diff = (long)maxExclusiveValue - minValue;
long upperBound = uint.MaxValue / diff * diff;
} while (ui >= upperBound);
return (int)(minValue + (ui % diff));
private uint GetRandomUInt()
var randomBytes = GenerateRandomBytes(sizeof(uint));
return BitConverter.ToUInt32(randomBytes, 0);
private byte[] GenerateRandomBytes(int bytesNumber)
byte[] buffer = new byte[bytesNumber];