HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Probability Chance

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
chanceprobabilitystackoverflow

Problem

I'm creating a game and I need some properties to work as percent chance to hit/dodge. I implemented a small testing program that takes percent chance and the amount of hitting attempts as input and outputs the successful hits, failed hits, and overall success rate.

My game will have decimal percents (1.5, 54.8..) The way the code works is pretty simple, let's say you have 50 percent critical chance.

-
We take the % chance as input and multiply it by 10 so we can work properly with decimal %'s.

-
We put all the numbers from 1 up to your current % in a list.

-
We generate a random number from 1 - 1000 using RNGCryptoServiceProvider. The range here is 1 - 1000 because we previously multiplied the user % chance by 10 like this we equalize the sides.

-
Than we compare if the random number occurs in the list of our successful hits.

My main concern is if the program's generated numbers are random enough.

The main program:

private static void Main()
    {
        while (true)
        {
            Console.Write("Percent Chance : ");
            double percentChance = double.Parse(Console.ReadLine())*10;
            Console.Write("Hit count : ");
            double hitCount = int.Parse(Console.ReadLine());
            List criticalHits = new List();
            for (int i = 1; i  criticalHit == hitThatIsCritical))
                {
                    Console.WriteLine("Hit #{0} was a critical", i);
                    succesCritCount++;
                }
                else
                {
                    Console.WriteLine("Hit #{0} was not a critical", i);
                    failedCritCount++;
                }
            }
            double succesRate = succesCritCount/hitCount;
            Console.WriteLine("Succesful Hits : {0}",succesCritCount);
            Console.WriteLine("Failed Hits : {0}", failedCritCount);
            Console.WriteLine("Succes Rate : {0}",succesRate);
        }
    }


RngCrypto class:

```
using System;
using S

Solution

I don't know much about making numbers random enough, but I have some other suggestions.

Instead of checking if a range of integers contains a number you can just check if the number is >= the beginning of the range and <= the end of the range.

So you can replace this:

if(criticalHits.Any(criticalHit => criticalHit == hitThatIsCritical))


With this:

if(((int)percentChance) <= hitThatIsCritical)


Then you can get rid of this:

List criticalHits = new List();
for (int i = 1; i <= percentChance; i++)
{
    criticalHits.Add(i);
}


In the case where you do need to declare a range of integers, you can do so like this:

List criticalHits = Enumerable.Range(1, percentChance).ToList();


Note that the second parameter is the count instead of the end number, so you'll have to keep that in mind when replacing a for loop.

You probably shouldn't hard code the logic about things being multiplied by 10. Instead you should use a constant for 10. That way it's easier to modify if you want to change what things get multiplied by and it's easier to tell what the code is doing. I'm going to use the _cConstantName naming convention for constants, but you can use another one if you want.

First declare the constant:

private const int _cPrecisionFactor = 10;


Then you can replace the following two lines:

double percentChance = double.Parse(Console.ReadLine())*10;


and:

int hitThatIsCritical = rnd.Next(1, 1001);


with:

double percentChance = double.Parse(Console.ReadLine()) * _cPrecisionFactor;


and:

int hitThatIsCritical = rnd.Next(1, 100 * _cPrecisionFactor + 1);


You should also validate the input by using TryParse() instead of Parse() and looping until you get a valid number.

Code Snippets

if(criticalHits.Any(criticalHit => criticalHit == hitThatIsCritical))
if(((int)percentChance) <= hitThatIsCritical)
List<int> criticalHits = new List<int>();
for (int i = 1; i <= percentChance; i++)
{
    criticalHits.Add(i);
}
List<int> criticalHits = Enumerable.Range(1, percentChance).ToList();
private const int _cPrecisionFactor = 10;

Context

StackExchange Code Review Q#128935, answer score: 3

Revisions (0)

No revisions yet.