I need some help with percent chance in C# code. Let's say i have for loop from 1 to 100 and in that loop i have an "if" code that i want to be executed 70% times (on random). How would i achieve that? So something like this:
static void Main(string[] args)
{
var clickPercentage = 70;
for (int i = 0; i < 100; i++)
{
if (chance)
{
//do 70% times
}
}
}
So for top example i would want if code to be hit with a chance of 70%, about 70 times for my example.
Things i tried: (nowhere near 70%, more like 1 or 2% chance)
static void Main(string[] args)
{
var clickPercentage = 70;
for (int i = 0; i < 100; i++)
{
var a = GetRadnomNumber(1, clickPercentage);
var b = GetRadnomNumber(1, 101);
if (a <= b)
{
Console.WriteLine($"Iteracija {i} - {b} <= {a}");
}
}
}
public static int GetRadnomNumber(int min, int max)
{
var random = new Random();
var retVal = random.Next(min, max);
return retVal;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With