public void PermutationsShouldBeUnbiased(int count)
        {
            // Arrange
            var data         = Enumerable.Range(1, count).ToArray();
            var permutations = PermutationLexicographicOrdering.EnumeratePermutations(data)
                               .ToDictionary(Stringify, x => 0);

            var random  = new RandomWithSeed();
            var shuffle = new FisherYatesShuffle(random);

            var idealNumberOfOccurrences = 10000;
            var trials = permutations.Count * idealNumberOfOccurrences;

            // Act
            for (var i = 0; i < trials; ++i)
            {
                var tmp = Enumerable.Range(1, count).ToArray();
                shuffle.Shuffle(tmp);
                permutations[Stringify(tmp)]++;
            }

            // Assert
            var deviation  = 0.04;
            var maxAllowed = (int)(idealNumberOfOccurrences * (1 + deviation));
            var minAllowed = (int)(idealNumberOfOccurrences * (1 - deviation));

            foreach (var pair in permutations)
            {
                var occurences = pair.Value;
                var message    = $"Expected permutation to be yielded {minAllowed} <= x <= {maxAllowed} times, " +
                                 $"but was {occurences}. Seed: {random.Seed}";

                Assert.True(occurences >= minAllowed && occurences <= maxAllowed, message);
            }
        }
Exemplo n.º 2
0
        public BitcoinNode(Network network, RandomWithSeed rnd)
        {
            RandomUtils.Random = rnd;
            Network            = network;

            var key  = new Key(rnd.GetNextBytes(32));
            var code = rnd.GetNextBytes(32);

            _extKey    = new ExtKey(key, code);
            _extPubKey = _extKey.Derive(DefaultAccountKeyPath).Neuter();
            _keyDepth  = 0;
            ////////////////

            _blocks       = new List <Block>();
            _blockIndex   = new Dictionary <uint256, Block>();
            _mempool      = new List <Transaction>();
            _txIndex      = new Dictionary <uint256, Transaction>();
            _txBlock      = new Dictionary <Transaction, Block>();
            _utxos        = new Dictionary <OutPoint, TxOut>();
            _scriptKeyMap = new Dictionary <Script, PubKey>();

            var genesisBlock = network.GetGenesis();

            AddBlock(genesisBlock);
        }