Last active
August 29, 2015 14:07
-
-
Save dck-jp/00d89159c5ec11190390 to your computer and use it in GitHub Desktop.
PlayDice
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
N=1000 : p=0.16750418760469 | |
N=10000 : p=0.167664269067619 | |
N=100000 : p=0.166234180739775 | |
N=1000000 : p=0.166660555779621 | |
N=10000000 : p=0.166789565558048 | |
Press any key to continue . . . |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
namespace test | |
{ | |
class Program | |
{ | |
static int seed; | |
public static void Main(string[] args) | |
{ | |
seed = Environment.TickCount; | |
var NList = new List<int>(){ 1000,10000,100000,1000000,10000000}; | |
NList.ForEach( N => { | |
var p = PlayDice(N); | |
Console.WriteLine(String.Format("N={0} : p={1}", N, p.ToString())); | |
}); | |
Console.Write("Press any key to continue . . . "); | |
Console.ReadKey(true); | |
} | |
static double PlayDice(int N) | |
{ | |
var students = Enumerable.Range(0, N); | |
var sum = students.AsParallel().Sum(_ => GetNumberOfTrial()); | |
return (double)N / sum; | |
} | |
static int GetNumberOfTrial() { | |
var numOfTrial = 0; | |
var rnd = new Random(seed++); | |
for (; ;){ | |
var x = rnd.Next(6); | |
numOfTrial++; | |
if(x == 0) break; | |
} | |
return numOfTrial; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment