Skip to content

Instantly share code, notes, and snippets.

@vladdeSV
Created July 9, 2016 10:21
Show Gist options
  • Save vladdeSV/d41e974df03f2fc56650895ca2a887bc to your computer and use it in GitHub Desktop.
Save vladdeSV/d41e974df03f2fc56650895ca2a887bc to your computer and use it in GitHub Desktop.
/**
* Simple random checking.
*/
module slump;
import std.random : uniform;
///Returns: One out of `oneOutOf´ probability's chance of returning true
bool probabilityOf(int oneOutOf)
{
return uniform(0, oneOutOf) == 0;
}
///Returns: random number within 0 and `count`
int random(int count)
{
return uniform(0, count);
}
///Returns: random element in array
A random(A)(A[] arr)
{
return arr[random($)];
}
///Returns: randomly true or false
bool coin()
{
return cast(bool) uniform(0, 2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment