public static LCG GetShuffleFunction( BigInteger deckSize, IList <ShuffleInstruction> shuffleInstructions) { // Combine all shuffle instructions into one LCG var result = LCG.GetIdentity(deckSize); var dealIntoNewStack = GetShuffleFunctionDealIntoNewStack(deckSize); foreach (var shuffleInstruction in shuffleInstructions) { if (ShuffleTechnique.DealIntoNewStack.Equals(shuffleInstruction.Technique)) { result = LCG.Compose(result, dealIntoNewStack); } else if (ShuffleTechnique.CutNCards.Equals(shuffleInstruction.Technique)) { var cutNCards = GetShuffleFunctionCutNCards(shuffleInstruction.ShuffleParameter, deckSize); result = LCG.Compose(result, cutNCards); } else if (ShuffleTechnique.DealWithIncrementN.Equals(shuffleInstruction.Technique)) { var dealWithIncrementN = GetShuffleFunctionDealWithIncrementN(shuffleInstruction.ShuffleParameter, deckSize); result = LCG.Compose(result, dealWithIncrementN); } } return(result); }
public static BigInteger ShuffleCard( BigInteger startCardIndex, BigInteger deckSize, BigInteger numberOfShuffles, IList <ShuffleInstruction> shuffleInstructions, bool runBackwards) { var shuffleFunction = GetShuffleFunction(deckSize, shuffleInstructions); // Compose the shuffle function into itself once for each shuffle pass shuffleFunction = ExponentiationHelper.GetExponentiationByPowers( x: shuffleFunction, exponent: numberOfShuffles, MultiplyFunction: LCG.Compose, identity: LCG.GetIdentity(deckSize)); // If we are running backwards, then use the inverse of the // shuffle function if (runBackwards) { shuffleFunction = shuffleFunction.GetInverse(); } var endCardIndex = shuffleFunction.Evaluate(startCardIndex); return(endCardIndex); }
public void Initialize(LCG random, InteractiveDungeonObject interactiveObjectContainer, SpawnableContainer spawnKeyframes, TrapContainer trapContainer, MapPainter mapPainter) { _timer = new Timer(); _random = random; _interactiveObjectContainer = interactiveObjectContainer; _spawnKeyframes = spawnKeyframes; _trapContainer = trapContainer; _mapPainter = mapPainter; }
public void LCG_Next_Test() { LCG target = new LCG(); uint max = 10; target.Srand(2); uint result = target.Next(max); Assert.IsTrue(result >= 0, "Wartosc losowa LCG jest mniejsza od 0!"); Assert.IsTrue(result <= max, "Wartosc losowa LCG jest większa niż podana wartość maksymalna!"); }
public DRandom(bool useLCGenerator, bool fixedRange, int minValue, int maxValue) { useLCG = useLCGenerator; if (useLCG) { generator = new LCG(); } isFixedRange = fixedRange; if (isFixedRange) { if (minValue > maxValue) { throw new ArgumentException("Error: Invalid arguments for DRandom(): minValue cannot be greater than maxValue."); } fixedRangeSamples = new int[SAMPLE_SIZE]; fixedRangeMin = minValue; fixedRangeMax = maxValue; } GenerateSamples(); }
public void LCGCorrectness() { var lcg = new LCG(123, 456, 1); Assert.Equal(579u, lcg.Next()); Assert.Equal(71673u, lcg.Next()); Assert.Equal(8816235u, lcg.Next()); Assert.Equal(1084397361u, lcg.Next()); Assert.Equal(236889683u, lcg.Next()); Assert.Equal(3367627689u, lcg.Next()); Assert.Equal(1901345787u, lcg.Next()); Assert.Equal(1937298273u, lcg.Next()); Assert.Equal(2064486755u, lcg.Next()); Assert.Equal(528800857u, lcg.Next()); Assert.Equal(617996427u, lcg.Next()); Assert.Equal(2999116945u, lcg.Next()); Assert.Equal(3819164531u, lcg.Next()); Assert.Equal(1605802505u, lcg.Next()); Assert.Equal(4240180251u, lcg.Next()); Assert.Equal(1851128513u, lcg.Next()); lcg.Seed(2); Assert.Equal(702u, lcg.Next()); Assert.Equal(86802u, lcg.Next()); Assert.Equal(10677102u, lcg.Next()); Assert.Equal(1313284002u, lcg.Next()); Assert.Equal(2620142750u, lcg.Next()); Assert.Equal(155011506u, lcg.Next()); Assert.Equal(1886546510u, lcg.Next()); Assert.Equal(116987202u, lcg.Next()); Assert.Equal(1504524414u, lcg.Next()); Assert.Equal(372909650u, lcg.Next()); Assert.Equal(2918214446u, lcg.Next()); Assert.Equal(2458091746u, lcg.Next()); Assert.Equal(1697574494u, lcg.Next()); Assert.Equal(2643233010u, lcg.Next()); Assert.Equal(2995113486u, lcg.Next()); Assert.Equal(3326739074u, lcg.Next()); }
public void GetExponentiationByPowersLCFTest() { var testData = new List <Tuple <LCG, BigInteger, Func <LCG, LCG, LCG>, LCG> >() { // f(f(f(x))) where f(x) = 2*x + 3 (mod 5) // f(f(f(x))) = f(f(2x + 3)) // = f(2*(2x + 3) + 3) // = f(4x + 6 + 3) // = f(4x + 9) // = 2*(4x + 9) + 3 // = 8x + 18 + 3 // = 8x + 21 (mod 5) Tuple.Create( new LCG(2, 3, 5), (BigInteger)0, (Func <LCG, LCG, LCG>)LCG.Compose, LCG.GetIdentity(5)), Tuple.Create( new LCG(2, 3, 5), (BigInteger)1, (Func <LCG, LCG, LCG>)LCG.Compose, new LCG(2, 3, 5)), Tuple.Create( new LCG(2, 3, 5), (BigInteger)3, (Func <LCG, LCG, LCG>)LCG.Compose, new LCG(8, 21, 5)), }; foreach (var testExample in testData) { var result = ExponentiationHelper.GetExponentiationByPowers <LCG>( x: testExample.Item1, exponent: testExample.Item2, MultiplyFunction: testExample.Item3, identity: LCG.GetIdentity(testExample.Item1.M)); Assert.Equal(testExample.Item4, result); } }
public Map(Tilemap floors, Tilemap walls, Tilemap pits, LCG random) { _drawCells = true; _drawDelaunay = false; _drawGabriel = false; _drawEMST = false; _drawCorridors = true; _drawLayout = true; _drawBounds = false; _floors = floors; _walls = walls; _pits = pits; _random = random; InteractiveObjects = new List <GameObject>(); Enemies = new List <GameObject>(); ChokePoints = new List <BoundsInt>(); StartToGoalPath = new List <LineSegment2D>(); CollisionMapCollider = GameObject.Find("CollisionMap").GetComponent <TilemapCollider2D>(); }
private static void Main(string[] args) { var random = new System.Random(1234); Vector3 GetPoint() { float Get() { return((float)Math.Round((1.0d - 2.0d * random.NextDouble()) * 10)); } return(new Vector3(Get(), Get(), Get())); } var points = Enumerable.Range(0, 10000).Select(s => GetPoint()).ToArray(); var numEdges = OrientedBoundingBox.NumEdges; var numFaces = OrientedBoundingBox.NumFaces; var numVertices = OrientedBoundingBox.NumVertices; #if UNITY_EDITOR || UNITY var box = new OrientedBoundingBox(Vector3.zero, Vector3.zero, Vector3.right, Vector3.up, Vector3.forward); #else var box = new OrientedBoundingBox(Vector3.Zero, Vector3.Zero, Vector3.Right, Vector3.Up, Vector3.Forward); #endif foreach (var point in points) { box.Enclose(point); } #if UNITY_EDITOR || UNITY var b1 = box.Contains(Vector3.zero); #else var b1 = box.Contains(Vector3.Zero); #endif var b2 = box.Contains(new Vector3(100, 100, 100)); var c1 = box.CornerPoint(0); var c2 = box.CornerPoint(7); var f1 = box.FacePoint(0, 0.0f, 0.0f); var f2 = box.FacePoint(0, 1.0f, 1.0f); var p1 = box.PointInside(0.0f, 0.0f, 0.0f); var p2 = box.PointInside(1.0f, 1.0f, 1.0f); var p3 = box.PointInside(0.5f, 0.5f, 0.5f); #if UNITY_EDITOR || UNITY box.Translate(Vector3.one * +2); box.Translate(Vector3.one * -2); box.Scale(Vector3.zero, Vector3.one * 0.5f); box.Scale(Vector3.zero, Vector3.one * 2.0f); var d1 = box.Distance(Vector3.one * 20.0f); var d2 = box.Distance(Vector3.one * 30.0f); #else box.Translate(Vector3.One * +2); box.Translate(Vector3.One * -2); box.Scale(Vector3.Zero, Vector3.One * 0.5f); box.Scale(Vector3.Zero, Vector3.One * 2.0f); var d1 = box.Distance(Vector3.One * 20.0f); var d2 = box.Distance(Vector3.One * 30.0f); #endif var e1 = box.PointOnEdge(0, 0.0f); var e2 = box.PointOnEdge(0, 1.0f); var e3 = box.PointOnEdge(0, 0.5f); var l1 = box.Edge(0); var l2 = box.Edge(1); var m1 = box.WorldToLocal(); var m2 = box.LocalToWorld(); var n1 = box.FacePlane(0); var n2 = box.FacePlane(1); var x1 = OrientedBoundingBox.OptimalEnclosing(points); var x2 = OrientedBoundingBox.BruteEnclosing(points); LCG lcg = new LCG(); lcg.IntFast(); lcg.Float(); lcg.Int(); lcg.Int(0, 10); #if UNITY_EDITOR || UNITY OrientedBoundingBox obb = new OrientedBoundingBox(Vector3.zero, Vector3.one, Vector3.right, Vector3.up, Vector3.forward); #else OrientedBoundingBox obb = new OrientedBoundingBox(Vector3.Zero, Vector3.One, Vector3.Right, Vector3.Up, Vector3.Forward); #endif obb.RandomPointOnSurface(lcg); }
static void Main() { while (true) // Continue the game untill the user does want to anymore... { int threads = 3; int threadLoad = 10; ulong initialValue = 7; int Method = 1; Console.WriteLine("Usage: Threads <num> Load <num> Seed <num> Method <num>"); Console.WriteLine("Method: 0-Tree,1-Leapfrog,2-Modified Leapfrog. Argument+1 - speedtest."); String[] args = Console.ReadLine().Split(' '); bool test_threads = int.TryParse(args[0], out threads); bool test_load = int.TryParse(args[1], out threadLoad); bool test_seed = ulong.TryParse(args[2], out initialValue); bool test_Method = int.TryParse(args[3], out Method); if (test_threads == false || test_load == false || test_seed == false || test_Method == false) { System.Console.WriteLine("Please enter a numeric argument."); System.Console.WriteLine("Usage: Threads <num> Load <num> Seed <num> Method <num>"); } // One event is used for each Fibonacci object //LCGLeapfrog[] LCGLeapfrogArray = new LCGLeapfrog[threads]; TimeSpan?start = DateTime.Now.TimeOfDay; TimeSpan?end = null; if (Method == 1) { ManualResetEvent[] doneEvents = new ManualResetEvent[threads]; LCGLeapfrog[] lcgArray = new LCGLeapfrog[threads]; //Pradinė seka iš n-gijų sk. narių LCGLeapfrog initialSeq = new LCGLeapfrog(initialValue, threads); initialSeq.Calculateinitial(threads); for (int i = 0; i < threads; i++) { doneEvents[i] = new ManualResetEvent(false); //Inicializuojam su pradinės sekos nariais //laipsnį n parenkam > gijų sk., kad sekos nesidublikuotu ulong seed = initialSeq.Result[i]; LCGLeapfrog f = new LCGLeapfrog(seed, threadLoad, threads, doneEvents[i]); lcgArray[i] = f; ThreadPool.QueueUserWorkItem(f.ThreadPoolCallback, i); } WaitHandle.WaitAll(doneEvents); Console.WriteLine("All calculations are complete."); // Display the results... for (int i = 0; i < threads; i++) { LCGLeapfrog f = lcgArray[i]; for (int j = 0; j < f.Result.Length; j++) { Console.WriteLine("Thread({0}) = {1}", i, f.Result[j]); } } } else if (Method == 0) { ManualResetEvent[] doneEvents = new ManualResetEvent[threads]; LCG[] lcgArray = new LCG[threads]; //Sugeneruojam pradinę seką su n-gijų skaičius narių LCG2 initialSeq = new LCG2(initialValue, threads); initialSeq.Calculateinitial(threads); //Kiekvienai gijai, generuojam naują seką, //pradines reikšmes parinkdami iš pradinės sekos for (int i = 0; i < threads; i++) { doneEvents[i] = new ManualResetEvent(false); //Pradinė reikšmė ulong seed = initialSeq.Result[i]; //Generuojam seką LCG f = new LCG(seed, threadLoad, doneEvents[i]); lcgArray[i] = f; ThreadPool.QueueUserWorkItem(f.ThreadPoolCallback, i); } // Wait for all threads in pool to calculation... WaitHandle.WaitAll(doneEvents); Console.WriteLine("All calculations are complete."); // Display the results... for (int i = 0; i < threads; i++) { LCG f = lcgArray[i]; for (int j = 0; j < f.Result.Length; j++) { Console.WriteLine("Thread({0}) = {1}", i, f.Result[j]); } } } else if (Method == 2) { ManualResetEvent[] doneEvents = new ManualResetEvent[threads]; LCG[] lcgArray = new LCG[threads]; //Pradinė seka su tarpais n-narių kiekis sekoje LCGLeapfrog initialSeq = new LCGLeapfrog(initialValue, threads, threadLoad); initialSeq.Calculateinitial(threads); for (int i = 0; i < threads; i++) { doneEvents[i] = new ManualResetEvent(false); //Pradinės reikšmės iš išretintos sekos //Toliau kiekviena gija generuoja nuosekliai ulong seed = initialSeq.Result[i]; LCG f = new LCG(seed, threadLoad, doneEvents[i]); lcgArray[i] = f; ThreadPool.QueueUserWorkItem(f.ThreadPoolCallback, i); } WaitHandle.WaitAll(doneEvents); Console.WriteLine("All calculations are complete."); // Display the results... for (int i = 0; i < threads; i++) { LCG f = lcgArray[i]; for (int j = 0; j < f.Result.Length; j++) { Console.WriteLine("Thread({0}) = {1}", i, f.Result[j]); } } } else if (Method == 11) { TimeSpan?previousTime = null; for (int k = 1; k < threads; k++) { start = DateTime.Now.TimeOfDay; ManualResetEvent[] doneEvents = new ManualResetEvent[k]; LCGLeapfrog[] lcgArray = new LCGLeapfrog[k]; LCGLeapfrog initialSeq = null; initialSeq = new LCGLeapfrog(initialValue, k); initialSeq.Calculateinitial(k); Console.WriteLine("launching {0} tasks...", k); for (int i = 0; i < k; i++) { doneEvents[i] = new ManualResetEvent(false); ulong seed = 0; seed = initialSeq.Result[i]; LCGLeapfrog f = new LCGLeapfrog(seed, threadLoad / k, k, doneEvents[i]); lcgArray[i] = f; ThreadPool.QueueUserWorkItem(f.ThreadPoolCallback, i); } WaitHandle.WaitAll(doneEvents); end = DateTime.Now.TimeOfDay; if (previousTime != null) { Console.WriteLine("Speedup: {0}%", ((previousTime.Value.TotalMilliseconds / (end - start).Value.TotalMilliseconds) - 1) * 100); } previousTime = end - start; Console.WriteLine("Time taken: {0}", previousTime); } } else if (Method == 21) { TimeSpan?previousTime = null; for (int k = 1; k < threads; k++) { ManualResetEvent[] doneEvents = new ManualResetEvent[k]; LCG[] lcgArray = new LCG[k]; LCGLeapfrog initialSeq = null; initialSeq = new LCGLeapfrog(initialValue, k, threadLoad / k); initialSeq.Calculateinitial(k); Console.WriteLine("launching {0} tasks...", k); for (int i = 0; i < k; i++) { doneEvents[i] = new ManualResetEvent(false); ulong seed = 0; seed = initialSeq.Result[i]; LCG f = new LCG(seed, threadLoad / k, doneEvents[i]); lcgArray[i] = f; ThreadPool.QueueUserWorkItem(f.ThreadPoolCallback, i); } WaitHandle.WaitAll(doneEvents); Console.WriteLine("All calculations are complete."); end = DateTime.Now.TimeOfDay; if (previousTime != null) { Console.WriteLine("Speedup: {0}%", ((previousTime.Value.TotalMilliseconds / (end - start).Value.TotalMilliseconds) - 1) * 100); } previousTime = end - start; Console.WriteLine("Time taken: {0}", previousTime); } } end = DateTime.Now.TimeOfDay; Console.WriteLine("Time taken: {0}", end - start); while (true) // Continue asking until a correct answer is given. { Console.Write("Do you want to play again [Y/N]?"); string answer = Console.ReadLine().ToUpper(); if (answer == "Y") { break; // Exit the inner while-loop and continue in the outer while loop. } if (answer == "N") { return; // Exit the Main-method. } } //Console.ReadKey(); } }
public void RandomToFile(int seed, int range, int numberOfPRNGs) { String file = ""; StringBuilder stringBuilder = new StringBuilder(); String content = ""; AbstractRandom randomAbstract = new LCG(); Random random = new Random(seed); TimerManager.ClearMessage(); for (int i = 0; i < 3; i++) { stringBuilder.Clear(); if (i == 0) { randomAbstract = new LCG(); randomAbstract.Srand((uint)seed); file = "Pseudo Random Numbers LCG.csv"; } else if (i == 1) { randomAbstract = new LFG(); randomAbstract.Srand((uint)seed); file = "Pseudo Random Numbers LFG.csv"; } else if (i == 2) { randomAbstract = new MSM(); randomAbstract.Srand((uint)seed); file = "Pseudo Random Numbers MSM.csv"; } TimerManager.TimerStart(file); for (int j = 0; j < numberOfPRNGs; j++) { stringBuilder.Append(randomAbstract.Next((uint)range)); stringBuilder.Append(Environment.NewLine); } TimerManager.TimrStop(); content = stringBuilder.ToString(); WriteToFile(file, content); } file = "Pseudo Random Numbers Random C#.csv"; stringBuilder.Clear(); TimerManager.TimerStart(file); for (int j = 0; j < numberOfPRNGs; j++) { stringBuilder.Append(random.Next(range)); stringBuilder.Append(Environment.NewLine); } TimerManager.TimrStop(); MessageBox.Show("Pliki zostały wygenerowane.", "Generowanie plików"); content = stringBuilder.ToString(); WriteToFile(file, content); }