- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
using System;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication32
{
class Program
{
static readonly Random Random = new Random(DateTime.Now.Millisecond);
private static int _counterTrue = 0;
private static int _counterFalse = 0;
private const int _MaxRand = int.MaxValue;
private const int testLimit = 10000000;
static void Main(string[] args)
{
Parallel.For(0, testLimit, (i) => Test());
Console.WriteLine(_counterTrue);
Console.WriteLine(_counterFalse);
Console.WriteLine(_counterFalse/(float)(testLimit));
Console.ReadKey();
}
static private void Test()
{
var first = Random.Next(_MaxRand);
var second = Random.Next(_MaxRand);
if (first == second)
{
second = Random.Next(_MaxRand);
}
if (first == second)
{
Interlocked.Increment(ref _counterTrue);
}
else
{
Interlocked.Increment(ref _counterFalse);
}
}
}
}