C# Challenge - Calculating the average of a random number array

Calculate it by yourself… WHAT?! Let’s automate that!

Timo Schmid
.Net Programming
4 min readJun 2, 2021

--

Photo by Edge2Edge Media on Unsplash

There are some times where you have a large amount of numbers and you just need the average of all these numbers. Hm… What to do? Calculate everything by yourself? Type in every number into a calculator and divide the number by the actual amount of numbers? Well, this all are valid solutions. However, they can take some time…

Let’s randomize the numbers and let a method calculate the average!

Getting started
First things first. Let’s take a look at what we should do:

The task for today. Calculating random numbers.

To make things a bit easier for us, let’s convert the task into pseudo-code:

I. Creating the variables
We need a total of three variables. All of them should be Serialized to be sure everything works as expected:

  • An array which stores the random numbers
  • A variable to store the total score of all numbers
  • A variable to store the calculated average score

As we are going to use full numbers only, we only need the data type int.

The variables we need!

II. Generating and calculating the scores
The task is to calculate random numbers and print out the average of them. As we don’t know how many numbers we will calculate, it is important to create a modular script which does not include any hard coded values in it.

Let’s go through all the steps we need to do together:

  • Every number in the array should be a random number between 0 and 100
  • The total score is every single random generated number. For that, we need to add the current index of the array to the _totalScore variable.
  • After every random number has been generated and added to the _totalScore, we need the average of all the numbers. To get it, we need to divide the _totalScore by the length of the array.

As the first two points are repeating themselves until the last array index has been reached, it’s wise to use a loop, or more specifically, a for-loop, for them.

Here is how the code will look like when we convert the tasks into code:

The method to calculate the total and the average score

III. Printing out the rank
The last thing we are asked to do is to print out a rank which is calculated by the average score. This is done pretty quickly by using four if-statements.

As much as I know, a switch statement cannot be used if you have more than only one value for triggering the same behavior. Let’s say Action A is triggered from a end result between 0 and 30. If the number is 27, Action B will get triggered. Otherwise, Action C will get triggered.
Action B could be implemented with a switch statement, but not A and C, as they have a range of numbers triggering them.

To not just write down “You scored Rank x”, let’s make the messages more personal and funny! You can also mess around with the type of output like I did. Rank C will print out a warning, whereas Rank F will get printed out as an error.

Here are my messages. Maybe you like them:

The method to print out the rank!

Oh, and before you forget the same thing I often do when I create custom methods for an action, call both methods in void Start().
Otherwise, the script will not calculate (or even generate numbers!) at all…

Don’t forget to actually CALL the methods!

IV. Finished end results
Lastly, attach the script to the GameObject you like to. Enter a number for the array length (or in other words, the amount of random numbers you want to have), hit Play, and let math do it’s job!

The working script!

As you can see, randomness really doesn’t like me. I got an F at both times. Well, at least the number got bigger, that’s a start, huh? And a sign that the script is working!

I hope I could help you out with this little article here.
Take care and until next time!

--

--

Timo Schmid
.Net Programming

The mission? Becoming a game developer! RPG is the dream! Writing down my journey here for me and for everyone interested. Thanks for showing interest :)