Пример #1
0
        public void GenerateDesiredQuantityOfRandomIntegers(string inputString, DesiredCpuUtilization desiredCpuUtilization, int desiredQuantityOfValues, BigInteger minIntValueInclusive, BigInteger maxIntValueExclusive)
        {
            _bagOfRandomIntegers = new ConcurrentBag <BigInteger>();

            int             threadUsage            = ThreadUsage(desiredCpuUtilization);
            ParallelOptions maxDegreeOfParallelism = new ParallelOptions {
                MaxDegreeOfParallelism = threadUsage
            };

            int iterationsPerThread = (desiredQuantityOfValues / threadUsage) + 1;

            Parallel.For(0, threadUsage, maxDegreeOfParallelism, (i) =>
            {
                inputString += i;
                PRNG prng    = new PRNG(inputString);

                var listOfRandomIntegers = prng.GenerateListOfEntropyValuesBigInteger(minIntValueInclusive, maxIntValueExclusive, iterationsPerThread);

                // ConcurrentBag.Concat will turn the concurrent bag into IEnumrable, therefore each byte array must be added to the bag invidually.
                foreach (BigInteger randomValue in listOfRandomIntegers)
                {
                    _bagOfRandomIntegers.Add(randomValue);
                }
            });

            while (_bagOfRandomIntegers.Count > desiredQuantityOfValues)
            {
                BigInteger randomValue;
                _bagOfRandomIntegers.TryTake(out randomValue);
            }
        }
Пример #2
0
        public void GenerateDesiredQuantityOfRandom32ByteArrays(string inputString, DesiredCpuUtilization desiredCpuUtilization, int quantityOfRandom32ByteArrays)
        {
            _bagOfRandomBytes = new ConcurrentBag <byte[]>();

            int             threadUsage            = ThreadUsage(desiredCpuUtilization);
            ParallelOptions maxDegreeOfParallelism = new ParallelOptions {
                MaxDegreeOfParallelism = threadUsage
            };

            int iterationsPerThread = (quantityOfRandom32ByteArrays / threadUsage) + 1;

            Parallel.For(0, threadUsage, maxDegreeOfParallelism, (i, parallelLoopState) =>
            {
                inputString += i;
                PRNG prng    = new PRNG(inputString);

                var listOfEntropy32ByteArrays = prng.GenerateListOf32ByteArrays(iterationsPerThread);

                // ConcurrentBag.Concat will turn the concurrent bag into IEnumrable, therefore each byte array must be added to the bag invidually.
                foreach (byte[] byteArray in listOfEntropy32ByteArrays)
                {
                    _bagOfRandomBytes.Add(byteArray);
                }
            });

            while (_bagOfRandomBytes.Count > quantityOfRandom32ByteArrays)
            {
                byte[] random32ByteArray;
                _bagOfRandomBytes.TryTake(out random32ByteArray);
            }
        }