Generate true or false boolean with probability
4 answers
You can use the built-in Random.NextDouble()
:
Returns a random floating point number that is greater than or equal to 0.0 and less than 1.0
Then you can check if this number is greater than the probability value:
static Random random = new Random();
public static void Main()
{
// call the method 100 times and print its result...
for(var i = 1; i <= 100; i++)
Console.WriteLine("Test {0}: {1}", i, ForgeItem(0.4));
}
public static bool ForgeItem(double probability)
{
var randomValue = random.NextDouble();
return randomValue <= probability;
}
Note that the same Random
instance must be used . Here is an example Scripts .
0
source to share