Skip to content

Instantly share code, notes, and snippets.

@jpf91
Created June 12, 2012 09:08
Show Gist options
  • Save jpf91/2916360 to your computer and use it in GitHub Desktop.
Save jpf91/2916360 to your computer and use it in GitHub Desktop.
///Seed using an InputRange
void seedRange(T)(T range) if(isInputRange!T && is(Unqual!(ElementType!T) == UIntType))
{
int j;
for(j = 0; j < n && !range.empty; ++j, range.popFront())
{
mt[j] = range.front;
}
mti = n;
if(range.empty && j < n)
throw new Exception("MersenneTwisterEngine.seedRange: Input range didn't provide enough"
" elements");
popFront();
}
unittest
{
Mt19937 gen;
//This is repeating one unpredictableSeed! Not a good idea in general, but OK for the unittest
bool thrown = false;
try
gen.seedRange(repeat(unpredictableSeed, 623));
catch(Exception)
thrown = true;
assert(thrown);
gen.seedRange(repeat(unpredictableSeed, 624));
//infinite Range
gen.seedRange(repeat(unpredictableSeed));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment