C
C#2y ago
Birdman

Strange result calculating probability of hit

I'm getting a strange result when running this function
int DamageHitBellCurveCalc() {
var randomValue = Random.value;
var finalValue = 0;
if (randomValue > 0.8623f) {
finalValue = 0;
} else if (randomValue > 0.7424f) {
finalValue = 1;
} else if (randomValue > 0.6379f) {
finalValue = 2;
} else if (randomValue > 0.5469f) {
finalValue = 3;
} else if (randomValue > 0.4676f) {
finalValue = 4;
} else if (randomValue > 0.3986f) {
finalValue = 5;
} else if (randomValue > 0.3384f) {
finalValue = 6;
} else if (randomValue > 0.2860f) {
finalValue = 7;
} else if (randomValue > 0.2404f) {
finalValue = 8;
} else if (randomValue > 0.2006f) {
finalValue = 9;
} else if (randomValue > 0.1660f) {
finalValue = 10;
} else if (randomValue > 0.1358f) {
finalValue = 11;
} else if (randomValue > 0.0867f) {
finalValue = 12;
} else if (randomValue > 0.0667f) {
finalValue = 13;
} else if (randomValue > 0.0493f) {
finalValue = 14;
} else if (randomValue > 0.0342f) {
finalValue = 15;
} else if (randomValue > 0.0210f) {
finalValue = 16;
} else if (randomValue > 0.0096f) {
finalValue = 17;
} else if (randomValue > 0.0045f) {
finalValue = 18;
} else if (randomValue > 0.0023f) {
finalValue = 19;
} else if (randomValue > 0.0012f) {
finalValue = 20;
}
return finalValue;
}
int DamageHitBellCurveCalc() {
var randomValue = Random.value;
var finalValue = 0;
if (randomValue > 0.8623f) {
finalValue = 0;
} else if (randomValue > 0.7424f) {
finalValue = 1;
} else if (randomValue > 0.6379f) {
finalValue = 2;
} else if (randomValue > 0.5469f) {
finalValue = 3;
} else if (randomValue > 0.4676f) {
finalValue = 4;
} else if (randomValue > 0.3986f) {
finalValue = 5;
} else if (randomValue > 0.3384f) {
finalValue = 6;
} else if (randomValue > 0.2860f) {
finalValue = 7;
} else if (randomValue > 0.2404f) {
finalValue = 8;
} else if (randomValue > 0.2006f) {
finalValue = 9;
} else if (randomValue > 0.1660f) {
finalValue = 10;
} else if (randomValue > 0.1358f) {
finalValue = 11;
} else if (randomValue > 0.0867f) {
finalValue = 12;
} else if (randomValue > 0.0667f) {
finalValue = 13;
} else if (randomValue > 0.0493f) {
finalValue = 14;
} else if (randomValue > 0.0342f) {
finalValue = 15;
} else if (randomValue > 0.0210f) {
finalValue = 16;
} else if (randomValue > 0.0096f) {
finalValue = 17;
} else if (randomValue > 0.0045f) {
finalValue = 18;
} else if (randomValue > 0.0023f) {
finalValue = 19;
} else if (randomValue > 0.0012f) {
finalValue = 20;
}
return finalValue;
}
21 Replies
Birdman
BirdmanOP2y ago
at finalValue = 12; there is a strangely large number of probabilities being created this is how i'm printing the results
var array = new int[21];


for (int i = 0; i < 100000; i++) {
array[DamageHitBellCurveCalc()]++;
}


for (int i = 0; i < array.Length; i++) {
print("NUMBER " + i + ": " + array[i]);
}
var array = new int[21];


for (int i = 0; i < 100000; i++) {
array[DamageHitBellCurveCalc()]++;
}


for (int i = 0; i < array.Length; i++) {
print("NUMBER " + i + ": " + array[i]);
}
i'm not sure why, maybe it's because the float starts with 0.0 or something?
Angius
Angius2y ago
Why do a Yandev-like wall of if-elses rather than using math?
Birdman
BirdmanOP2y ago
cause honestly i have no clue how to make a formula to calculate that
ACiDCA7
ACiDCA72y ago
where does random.value come from? will it update with every call? isnt the correct way to get a random number .Next() or so
Birdman
BirdmanOP2y ago
comes from the unity game engine, yes it gives a random value every call
ACiDCA7
ACiDCA72y ago
ah ok
Birdman
BirdmanOP2y ago
hmm, could it be a flaw with the random function itself 🤔
Angius
Angius2y ago
It's Unity, so it's entirely possible ¯\_(ツ)_/¯
Birdman
BirdmanOP2y ago
but technically there is nothing wrong with this code right? it should print out a proper bell curb of values?
Angius
Angius2y ago
I mean, the float values are pure magic So hard to tell at a glance what it'll do
Birdman
BirdmanOP2y ago
oh, nvm, got it the difference bewteen 0.1358f and 0.0867f is too large so naturally is assigns more values there, as it should okay, fixed 👍
Angius
Angius2y ago
1 occured 1208 times
0 occured 1397 times
2 occured 997 times
3 occured 960 times
14 occured 191 times
10 occured 370 times
7 occured 510 times
5 occured 678 times
9 occured 389 times
12 occured 460 times
15 occured 143 times
6 occured 588 times
11 occured 292 times
18 occured 45 times
13 occured 217 times
4 occured 836 times
8 occured 449 times
17 occured 106 times
16 occured 130 times
19 occured 28 times
20 occured 6 times
1 occured 1208 times
0 occured 1397 times
2 occured 997 times
3 occured 960 times
14 occured 191 times
10 occured 370 times
7 occured 510 times
5 occured 678 times
9 occured 389 times
12 occured 460 times
15 occured 143 times
6 occured 588 times
11 occured 292 times
18 occured 45 times
13 occured 217 times
4 occured 836 times
8 occured 449 times
17 occured 106 times
16 occured 130 times
19 occured 28 times
20 occured 6 times
var nums = new List<int>();

for (var i = 0; i < 10_000; i++)
{
nums.Add(BellCurve());
}

foreach (var vals in nums.GroupBy(x => x).Select(x => $"{x.Key} occured {x.Count()} times"))
{
Console.WriteLine(vals);
}

static int BellCurve() =>
Random.Shared.NextSingle() switch
{
> 0.8623f => 0,
> 0.7424f => 1,
> 0.6379f => 2,
> 0.5469f => 3,
> 0.4676f => 4,
> 0.3986f => 5,
> 0.3384f => 6,
> 0.2860f => 7,
> 0.2404f => 8,
> 0.2006f => 9,
> 0.1660f => 10,
> 0.1358f => 11,
> 0.0867f => 12,
> 0.0667f => 13,
> 0.0493f => 14,
> 0.0342f => 15,
> 0.0210f => 16,
> 0.0096f => 17,
> 0.0045f => 18,
> 0.0023f => 19,
> 0.0012f => 20,
_ => 0
};
var nums = new List<int>();

for (var i = 0; i < 10_000; i++)
{
nums.Add(BellCurve());
}

foreach (var vals in nums.GroupBy(x => x).Select(x => $"{x.Key} occured {x.Count()} times"))
{
Console.WriteLine(vals);
}

static int BellCurve() =>
Random.Shared.NextSingle() switch
{
> 0.8623f => 0,
> 0.7424f => 1,
> 0.6379f => 2,
> 0.5469f => 3,
> 0.4676f => 4,
> 0.3986f => 5,
> 0.3384f => 6,
> 0.2860f => 7,
> 0.2404f => 8,
> 0.2006f => 9,
> 0.1660f => 10,
> 0.1358f => 11,
> 0.0867f => 12,
> 0.0667f => 13,
> 0.0493f => 14,
> 0.0342f => 15,
> 0.0210f => 16,
> 0.0096f => 17,
> 0.0045f => 18,
> 0.0023f => 19,
> 0.0012f => 20,
_ => 0
};
And here I wrote a nice test app lol
Birdman
BirdmanOP2y ago
that's strange, why are the values in the random order, makes it hard to see discrepancies
Angius
Angius2y ago
Well, the values generated were random So the GroupBy() took the first value of the kind, and grouped the rest to it I can sort it, no problem And let's crank up the numbers to 100k
0 occured 13789 times
1 occured 12043 times
2 occured 10438 times
3 occured 9145 times
4 occured 7821 times
5 occured 7005 times
6 occured 6032 times
7 occured 5371 times
8 occured 4617 times
9 occured 4031 times
10 occured 3461 times
11 occured 2939 times
12 occured 4818 times
13 occured 1985 times
14 occured 1692 times
15 occured 1503 times
16 occured 1293 times
17 occured 1131 times
18 occured 541 times
19 occured 236 times
20 occured 109 times
0 occured 13789 times
1 occured 12043 times
2 occured 10438 times
3 occured 9145 times
4 occured 7821 times
5 occured 7005 times
6 occured 6032 times
7 occured 5371 times
8 occured 4617 times
9 occured 4031 times
10 occured 3461 times
11 occured 2939 times
12 occured 4818 times
13 occured 1985 times
14 occured 1692 times
15 occured 1503 times
16 occured 1293 times
17 occured 1131 times
18 occured 541 times
19 occured 236 times
20 occured 109 times
Birdman
BirdmanOP2y ago
yup, same result there's a missing value this 0.1095f then it works properly i missed it when i was copying from my excel lol
Angius
Angius2y ago
Where should it be?
Birdman
BirdmanOP2y ago
after the next biggest one after this > 0.1358f => 11, and then reorder the numbers that are output
Angius
Angius2y ago
Birdman
BirdmanOP2y ago
yup 👍
Angius
Angius2y ago
0 occured 13989 times
1 occured 12126 times
2 occured 10387 times
3 occured 9046 times
4 occured 7934 times
5 occured 6939 times
6 occured 6015 times
7 occured 5293 times
8 occured 4510 times
9 occured 3991 times
10 occured 3418 times
11 occured 3033 times
12 occured 2557 times
13 occured 2246 times
14 occured 1918 times
15 occured 1699 times
16 occured 1507 times
17 occured 1308 times
18 occured 1171 times
19 occured 544 times
20 occured 247 times
21 occured 122 times
0 occured 13989 times
1 occured 12126 times
2 occured 10387 times
3 occured 9046 times
4 occured 7934 times
5 occured 6939 times
6 occured 6015 times
7 occured 5293 times
8 occured 4510 times
9 occured 3991 times
10 occured 3418 times
11 occured 3033 times
12 occured 2557 times
13 occured 2246 times
14 occured 1918 times
15 occured 1699 times
16 occured 1507 times
17 occured 1308 times
18 occured 1171 times
19 occured 544 times
20 occured 247 times
21 occured 122 times
Birdman
BirdmanOP2y ago
👍
Want results from more Discord servers?
Add your server