Skip to content

Instantly share code, notes, and snippets.

@mattiasb
Last active September 15, 2019 02:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattiasb/9c4c5edd38d7d50fca94c08aa4baf4fc to your computer and use it in GitHub Desktop.
Save mattiasb/9c4c5edd38d7d50fca94c08aa4baf4fc to your computer and use it in GitHub Desktop.
Shuffler example
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