Created
May 3, 2009 10:03
-
-
Save PeterStegnar/105929 to your computer and use it in GitHub Desktop.
This file contains 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
///by Peter Stegnar | |
///SnippetID: 56dd1bc8-f917-440d-876b-ce4fa816f788 | |
///Fell free to use - with this header | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
public class MyClass | |
{ | |
private delegate int RandomFunction(); | |
public static void RunSnippet() | |
{ | |
Console.WriteLine("Rand(7) implementation with Rand(5)"); | |
//Console.WriteLine("Testing Rand(5): " + RandTest(1, 5, Rand5)); | |
//Console.WriteLine("Testing Rand(7) Native: " + RandTest(1, 7, Rand7Native)); | |
Console.WriteLine("Testing Rand(7): " + RandTest(1, 7, Rand7WithRand5)); | |
} | |
private static int Rand7WithRand5() | |
{ | |
//PUT YOU FAVOURITE ALGHORITHM HERE// | |
//1. Stackoverflow winner | |
int i; | |
do | |
{ | |
i = 5 * (Rand5() - 1) + Rand5(); // i is now uniformly random between 1 and 25 | |
} while (i > 21); | |
// i is now uniformly random between 1 and 21 | |
return i % 7 + 1; | |
//My 2 cents | |
//return (Rand5() + Rand5()) % 7 + 1; | |
} | |
private static int Rand5() | |
{ | |
Random random = new Random(); | |
return random.Next(1, 6); | |
} | |
private static int Rand7Native() | |
{ | |
Random random = new Random(); | |
return random.Next(1, 8); | |
} | |
private static bool RandTest(int minRange, int maxRange, RandomFunction randomFunction) | |
{ | |
List<int> listOfMandatoryRands = new List<int>(); | |
int i = 0; | |
do | |
{ | |
int randNum = randomFunction(); | |
if (randNum < minRange || randNum > maxRange) | |
{ | |
Console.WriteLine("--FALSE RAND: " + randNum); | |
return false; | |
} | |
//Console.WriteLine("--RandNum: " + randNum); | |
listOfMandatoryRands.Add(randNum); | |
i++; | |
} while (TestMandatoryFields(listOfMandatoryRands, minRange, maxRange)); | |
Console.WriteLine("--ITERATIONS: " + i); | |
return true; | |
} | |
private static bool TestMandatoryFields(IEnumerable<int> listOfMandatoryRands, int minRange, int maxRange) | |
{ | |
//listOfMandatoryRands = listOfMandatoryRands.Where(number => number >= minRange && number <= maxRange).Distinct() as List<int>; | |
Console.WriteLine("--DISTRIBUTION: " + DistributionPrinter(listOfMandatoryRands, minRange, maxRange)); | |
listOfMandatoryRands = (from number in listOfMandatoryRands where number >= minRange && number <= maxRange select number).Distinct(); | |
Console.WriteLine("--LIST: " + ArrayPrinter(listOfMandatoryRands)); | |
return listOfMandatoryRands.Count() != maxRange - minRange + 1; | |
} | |
private static string ArrayPrinter(IEnumerable<int> list) | |
{ | |
string arrayString = String.Empty; | |
foreach (int s in list) | |
{ | |
arrayString += s + " "; | |
} | |
return arrayString; | |
} | |
private static string DistributionPrinter(IEnumerable<int> list, int minRange, int maxRange) | |
{ | |
string distributionArray = String.Empty; | |
for (int i = minRange; i <= maxRange;i++) | |
{ | |
distributionArray += "[" + i + "]" + list.Count(number => number == i).ToString() + " "; | |
} | |
return distributionArray; | |
} | |
#region Helper methods | |
public static void Main() | |
{ | |
try | |
{ | |
RunSnippet(); | |
} | |
catch (Exception e) | |
{ | |
string error = string.Format("---\nThe following error occurred while executing the snippet:\n{0}\n---", e.ToString()); | |
Console.WriteLine(error); | |
} | |
finally | |
{ | |
Console.Write("Press any key to continue..."); | |
Console.ReadKey(); | |
} | |
} | |
private static void WL(object text, params object[] args) | |
{ | |
Console.WriteLine(text.ToString(), args); | |
} | |
private static void RL() | |
{ | |
Console.ReadLine(); | |
} | |
private static void Break() | |
{ | |
System.Diagnostics.Debugger.Break(); | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment