Skip to content

Instantly share code, notes, and snippets.

@asus4
Created October 3, 2013 14:21
Show Gist options
  • Save asus4/6810651 to your computer and use it in GitHub Desktop.
Save asus4/6810651 to your computer and use it in GitHub Desktop.
Random class. but inconsecutive value, like Lottery.
using System.Linq;
using System.Collections.Generic;
namespace ExMath {
/// <summary>
/// Lottery random.
/// </summary>
public class LotteryRandom <T> {
public IList<T> initialLots;
Stack<int> indexes;
public LotteryRandom(IList<T> initail_lots) {
if(initail_lots.Count == 0) {
throw new System.Exception("initial lots need more than 1");
}
this.initialLots = initail_lots;
RenewIndexes();
}
void RenewIndexes() {
var numbers = new List<int>(Enumerable.Range(0,initialLots.Count-1)).ToArray();
var numbers2 = numbers.OrderBy(i => System.Guid.NewGuid()).ToArray();
indexes = null;
indexes = new Stack<int>(numbers2);
}
public T GetNext() {
if(indexes.Count == 0) {
RenewIndexes();
}
int i = indexes.Pop();
return initialLots[i];
}
}
}
@asus4
Copy link
Author

asus4 commented Oct 3, 2013

usage

LotteryRandom lottery = LotteryRandom(new List(System.Linq.Enumerable.Range(0,10)));

lottery.GetNext();
lottery.GetNext();
...
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment