Your example changed to more C++ style of programming :
All numbers will now be in rang [0,n-1] to match array indices
if you need numbers [1,n] just add one but take care when using
the numbers as array indices which still will run from [0,n-1]
#include <iostream>
#include <vector>
#include <random> // c++'s random generation, srand is a "C" left over
// no : using namespace std; avoid using this line, specialy in larger problems
// unless you like nameclashes (https://www.learncpp.com/cpp-tutorial/2-9-naming-collisions-and-an-introduction-to-namespaces/)
int main()
{
//int highestNumber, outputLength, result; teach yourself to initialize values. (though not strictly necessary in this case)
int highestNumber = 0;
int outputLength = 0;
// int result = 0; result is not used in your code
std::cout << "What's the highest number you want to generate?: ";
std::cin >> highestNumber;
std::cout << "How long of a number sequence do you want to generate?: ";
std::cin >> outputLength;
std::cout << "Okay we will generate " << outputLength << " number(s) ranging from 1 to " << highestNumber << "!\n";
// TODO : CHECK USER INPUT, never trust it.
// for example what will hapen if user inputs outputLength <= 0?
// "C" style random generation switch to type from #include <random>
// https://en.cppreference.com/w/cpp/header/random
// srand(time(NULL));
// Use C++'s default random engine
// the random device generates a random seed (like time(NULL), but really random )
std::random_device seed;
std::default_random_engine generator(seed());
// a uniform distribution will give you numbers with equal distribution
// something rand() % highestNumber will not achieve
// nice thing is you can specify the range of numbers here and you don't
// have to do any calculations later.
std::uniform_int_distribution distribution(0, highestNumber-1);
// In modern C++ new/delete are hardly ever needed (unless you need to optimize memory managment)
// the datatype of choice for dynamically sized arrays, arrays for which the length is only
// know at runtime) std::vector is the datatype of choice.
// int* randomNumbers = new int[outputLength];
// initialize a vector with outputLength values and initialize them to 0
std::vector<int> randomNumbers(outputLength, 0);
// In C++ there are range based for loops, they can not pass beyond the
// end of the array, avoiding the troubles in this bit of code.
// for (int i = 1; i <= outputLength; i++) {
// result = rand() % highestNumber + 1;
// cout << result << ", ";
// randomNumbers[i] = result;
// }
// loop over the random numbers, use a reference to values in the array https://www.learncpp.com/cpp-tutorial/for-each-loops/
// so we can change the value (which was initialized to 0)
// https://www.learncpp.com/cpp-tutorial/references/
for (int& value : randomNumbers)
{
value = distribution(generator);
std::cout << value << ", ";
}
// int* numberCounter = new int[highestNumber];
std::vector<int> numberCounter(highestNumber, 0);
std::cout << "\nFrequency:\n";
/*
for (int i = 1; i <= highestNumber; i++) {
int temp2 = randomNumbers[i];
numberCounter[temp2 - 1] = numberCounter[temp2 - 1] + 1;
}
*/
// count frequencies no need to do intermediate counts of offsets in you
for (const int value : randomNumbers)
{
numberCounter[value]++;
}
/*
for (int i = 1; i <= highestNumber; i++) {
int frequency = 0;
frequency = numberCounter[i] / outputLength;
cout << numberCounter[i] << " occurs " << frequency << " of the time\n";
}
*/
// when you're going to divide numbers to show fractions
// you will need a floating point type.
// in this case we do use indices since we need values from one array
// to look into values of another array.
double size = static_cast<double>(outputLength);
for (std::size_t n = 0; n < highestNumber; ++n)
{
double frequency = static_cast<double>(numberCounter[n]) / size;
std::cout << n << " has a chance of " << frequency << " of occuring\n";
}
}