Having a repeatable 'random' sequence is useful in testing scenarios.
For example, you could use it in testing a game engine to ensure that an AI was correctly picking targets, or paths - even if it has a random path evaluation.
Here is a very simplistic example. No matter how many times you run this test, it will always pick the same three cards when given the same base random number generator.
This can be useful to ensure that the random number generator being used is the one supplied. And, for some reason if a new random number generator were introduced without altering the test, then the test would fail.
[TestMethod]
public void TestRandomPicking()
{
Random random = new Random(1);
Deck deck = new Deck(random);
Assert.AreEqual(3, deck.PickCard().Value);
Assert.AreEqual(1, deck.PickCard().Value);
Assert.AreEqual(5, deck.PickCard().Value);
}
public class Deck
{
public Deck()
{
_randomizer = new Random();
}
public Deck(Random randomizer)
{
_randomizer = randomizer;
}
Random _randomizer;
private List<Card> _cards = new List<Card>
{
new Card {Value = 1},
new Card {Value = 2},
new Card {Value = 3},
new Card {Value = 4},
new Card {Value = 5},
new Card {Value = 6},
new Card {Value = 7},
new Card {Value = 8},
new Card {Value = 9},
new Card {Value = 10}
};
private List<Card> Cards { get { return _cards; } }
public Card PickCard()
{
return Cards[_randomizer.Next(0, Cards.Count - 1)];
}
}
public class Card
{
public int Value { get; set; }
}