-
January 13th, 2025, 09:31 AM
#1
RndGenerator class in c++
I do not understand how to take my previous code in c and turn it into a C++ class due to my lack of knowledge on container classes. Would someone be kind enough to help me so I can study/test the final code. I've made everything obvious in what I am trying to achieve, also placed everything in the header for now.
Code:
#pragma once
#include <array>
#include <random>
#include <vector>
class RndGenerator
{
public:
RndGenerator(int MinNum, int MaxNum, int NumAmount, bool AllowRepeatedNum);
private:
void GenerateNumbers();
void ResetNumberStatus();
int PrintRndNumbers() const;
private:
int m_MinNum {0}; // The Minimum number allowed to be generated.
int m_MaxNum {0}; // The Maximum number allowed to be generated.
int m_NumAmount {0}; // Amount of numbers to be generated.
bool m_AllowRepeatedNum {false}; // Allow numbers to be repeated?, true/false.
//std::array<bool> m_NumberStatus; // Has number been generated already?, true/false.
std::vector<int> m_RndNumber; // The random number generated.
};
RndGenerator::RndGenerator(int MinNum, int MaxNum, int NumAmount, bool AllowRepeatedNum)
{
// Automatically seed numbers.
//srand((unsigned int) time(nullptr)); // Legacy c.
// How to in c++
//std::random_device rd; // Seed for the random number engine
//std::mt19937 gen(rd()); // Mersenne Twister engine
}
void RndGenerator::GenerateNumbers()
{
// How to with (using containers)
// ResetNumberStatus();
//
// int NumberValidCount = 0;
//
// while (NumberValidCount <= m_NumAmount)
// std::uniform_int_distribution<> Number(MinNum, MaxNum); // Range
// if (m_AllowRepeatedNum == false && m_NumberStatus[Number] == false ) // Not already generated
// push/emplace Number into m_RndNumber
// m_NumberStatus[Number] == true // Mark as generated
// NumberValidCount++
// else
// push/emplace Number into m_RndNumber
// NumberValidCount++
}
void RndGenerator::ResetNumberStatus()
{
// Use an iterator to reset m_NumberStatus[m_NumAmount] = false
}
int RndGenerator::PrintRndNumbers() const
{
// Use an iterator to print std::vector<int> m_RndNumber;
// Get the size of vector and iterate all numbers to print.
}
What the mind can conceive it can achieve.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|