﻿using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            var provider = new RNGCryptoServiceProvider();

            var bytes = new byte[8];

            var dict = new Dictionary<Int64, int>();

            int iterCount = 10000000;

            for (int i = 0; i < iterCount; i++)
            {
                provider.GetBytes(bytes);
                var number = BitConverter.ToInt64(bytes, 0);

                //number = 1;

                if (dict.ContainsKey(number))
                {
                    dict[number]++;
                }
                else
                {
                    dict.Add(number, 1);
                }
            }

            Console.WriteLine("Next numbers are dublicates (from {0} generation): ", iterCount);

            var col = dict.Where(x => x.Value > 1).ToList();

            if (col.Any())
            {
                foreach (var b in col)
                {
                    Console.WriteLine("number {0} was gen. {1} times", b.Key, b.Value);
                }

                var totalCount = col.Sum(x => x.Value - 1);

                Console.WriteLine("Chance to generate dublicate ({0} from {1}) = {2}%", totalCount, iterCount, (100.0 * totalCount) / iterCount);
            }
            else
            {
                Console.WriteLine("GZ! no dublicates!");
            }

        }
    }
}
