private static void TestRandomPicker() { var items = new List <RandomItem <string> > { new RandomItem <string>(0.6, "a"), new RandomItem <string>(0.1 / 3, "b"), new RandomItem <string>(0.1 / 3, "c"), new RandomItem <string>(0.1 / 3, "d"), new RandomItem <string>(0.3, "e") }; var rp = new RandomPicker <string>(items, 1337); var dict = new Dictionary <string, int>(); for (var i = 0; i < 10000; i++) { var pick = rp.Pick(); if (!dict.ContainsKey(pick.Item)) { dict[pick.Item] = 1; } else { dict[pick.Item]++; } } foreach (var i in dict) { Console.WriteLine(i.Key + ": " + i.Value); } }
public ActionResult WheelOfFortune(FormCollection collection) { int bet = collection["Bet"].AsInt(); GameResult result = new GameResult(); var items = new List <RandomItem <ISpinAction> > { new RandomItem <ISpinAction>(0.6, new BetMultiplier(0.5m)), new RandomItem <ISpinAction>(0.2, new BetMultiplier(1.1m)), new RandomItem <ISpinAction>(0.1, new BetMultiplier(0m)), new RandomItem <ISpinAction>(0.1 / 3, new BetMultiplier(1.5m)), new RandomItem <ISpinAction>(0.1 / 3, new BetMultiplier(2m)), new RandomItem <ISpinAction>(0.1 / 3, new BetMultiplier(3m)) }; Random rand = new Random(); var rp = new RandomPicker <ISpinAction>(items, rand.Next()); //TODO: rand.next gives the same order for each player, would be better to have a different order for each player var user = HttpContext.User.Identity as ClaimsIdentity; var userId = user.GetUserId(); using (var anacondaModel = new AnacondaModel()) { WalletDAO walletDAO = new WalletDAO(anacondaModel); if (walletDAO.Pay(userId, bet)) { result = rp.Pick().Item.Execute(new GameContext(bet)); } else { result = new GameResult() { Bet = bet, CreditsGained = 0, Status = ResultStatus.InsufficientCredits }; } var wallet = anacondaModel.Wallets.First(u => u.UserId == userId); wallet.Credits += result.CreditsGained; anacondaModel.SaveChanges(); } ViewBag.Bet = bet; return(View(result)); }
public string RandomPickPublicServiceUrl(Dictionary <string, List <string> > remoteServices, string serviceName) { var publicUrl = ""; List <string> serviceList = null; if (remoteServices != null && remoteServices.TryGetValue(serviceName, out serviceList)) { if (serviceList != null && serviceList.Count > 0) { var remoteInfoParts = RandomPicker.Pick <string>(serviceList).Split('|'); if (remoteInfoParts.Length >= 2) // name | url | key { var urls = remoteInfoParts[1].Split(','); if (urls.Length >= 2) { publicUrl = urls[1]; } } } } return(publicUrl); }
public List <SlotItem> GetRandomColumnFruit(Random rand) { var items = new List <RandomItem <SlotItem> > { new RandomItem <SlotItem>(0.15, new SlotItem("Banana", 0.5)), new RandomItem <SlotItem>(0.15, new SlotItem("Apple", 0.4)), new RandomItem <SlotItem>(0.1, new SlotItem("Gold", 1)), new RandomItem <SlotItem>(0.2, new SlotItem("Kiwi", 0.25)), new RandomItem <SlotItem>(0.2, new SlotItem("Raspberry", 0.2)), new RandomItem <SlotItem>(0.2, new SlotItem("Strawberry", 0.15)) }; var randomPicker = new RandomPicker <SlotItem>(items, rand.Next()); List <SlotItem> slotItems = new List <SlotItem>(); for (int i = 0; i < 3; i++) { slotItems.Add(randomPicker.Pick().Item); } return(slotItems); }