Last active
September 15, 2019 02:07
-
-
Save mattiasb/9c4c5edd38d7d50fca94c08aa4baf4fc to your computer and use it in GitHub Desktop.
Shuffler example
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
using System.Linq; | |
using System.Collections.Generic; | |
using System; | |
public class UniqueRandom { | |
private HashSet<int> intSet = new HashSet<int>(); | |
private Random rng; | |
public UniqueRandom(Random rng) { | |
this.rng = rng; | |
} | |
public int Next() { | |
int next; | |
do { | |
next = rng.Next(); | |
} while(intSet.Contains(next)); | |
intSet.Add(next); | |
return next; | |
} | |
} | |
public static class Shuffler { | |
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> enumerable, | |
Random rng) { | |
var urng = new UniqueRandom(rng); | |
return enumerable.OrderBy(a => urng.Next()); | |
} | |
} | |
public class ShuffleDeck { | |
public static void Main(string[] args) { | |
var rng = new Random(); | |
Console.WriteLine(String.Join("\n", deck.Shuffle(rng))); | |
} | |
// MagicFest Chiba – Winner deck | |
// https://www.channelfireball.com/magicfest-chiba-top-8-deck-lists/ | |
public static String[] deck = { | |
// Creatures | |
"Brineborn Cutthroat", | |
"Brineborn Cutthroat", | |
"Metropolis Sprite", | |
"Ferocious Pup", | |
"Ferocious Pup", | |
"Netcaster Spider", | |
"Netcaster Spider", | |
"Scuttlemutt", | |
"Tomebound Lich", | |
"Octoprophet", | |
"Thicket Crasher", | |
"Agent of Treachery", | |
"Meteor Golem", | |
// Planeswalkers | |
"Vivien, Arkbow Ranger", | |
// Spells | |
"Anticipate", | |
"Rabid Bite", | |
"Rabid Bite", | |
"Gift of Paradise", | |
"Pulse of Murasa", | |
"Winged Words", | |
"Wolfrider’s Saddle", | |
"Sleep Paralysis", | |
"Moldervine Reclamation", | |
// Lands | |
"Island", | |
"Island", | |
"Island", | |
"Island", | |
"Island", | |
"Island", | |
"Island", | |
"Island", | |
"Forest", | |
"Forest", | |
"Forest", | |
"Forest", | |
"Forest", | |
"Forest", | |
"Swamp", | |
"Jungle Hollow", | |
"Jungle Hollow" | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment