-
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.
-
January 13th, 2025, 11:05 AM
#2
Re: RndGenerator class in c++
Not a class but this illustrates how to use c++ random numbers
https://github.com/GeorgePimpleton/c...om_toolkit.hpp
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 14th, 2025, 06:23 AM
#3
Re: RndGenerator class in c++
Thanks, I will look at this.
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
|