Skip to content

Instantly share code, notes, and snippets.

@rofr
Created January 25, 2016 11:18
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 rofr/fcabe8bc3316f58cae7f to your computer and use it in GitHub Desktop.
Save rofr/fcabe8bc3316f58cae7f to your computer and use it in GitHub Desktop.
Is using a Random seed in unit tests a good idea?
[Test]
public void Length()
{
const string key = "a";
const int limit = 100000;
//seed is random but included in output if test fails
var seed = new Random().Next();
var r = new Random(seed);
int max = 0;
for (int i = 0; i < 1000; i++)
{
int index = r.Next(limit);
max = Math.Max(index, max);
_target.SetBit(key, index);
var actual = _target.StrLength(key);
Assert.AreEqual(max + 1, actual, "failed at iteration " + i + "using seed " + seed);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment