Skip to content
Snippets Groups Projects
Commit 5ae13801 authored by Jaime Arias's avatar Jaime Arias
Browse files

refactor: create a random function

parent 7f2da6b6
No related branches found
No related tags found
No related merge requests found
Pipeline #8734 passed with stages
in 8 minutes and 34 seconds
Checks: 'cppcoreguidelines-*,google-*,modernize-use-nullptr,performance-*,portability-*,readability-*'
Checks: 'cppcoreguidelines-*,google-*,modernize-use-nullptr,performance-*,portability-*,readability-*,-cppcoreguidelines-owning-memory'
WarningsAsErrors: true
FormatStyle: google
......@@ -12,6 +12,15 @@
using namespace std;
/**
* Generate a random number between 0 and max
* @param max - the maximum value
* @return the random number
*/
int random(int max) {
return static_cast<int>(arc4random() % max);
}
/**
* Generate a random string of a given length
* @param length - the length of the string
......@@ -26,7 +35,7 @@ string RandomString(int length) {
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
for (int i = 0; i < length; ++i) {
result += alphabet.at(rand() % alphabet_size);
result += alphabet.at(random(alphabet_size));
}
return result;
......@@ -57,7 +66,7 @@ std::vector<std::string> split(const std::string &str, char delimiter) {
*/
template <typename T>
T GetRandomElement(const std::set<T> &elements) {
return *std::next(elements.begin(), rand() % elements.size());
return *std::next(elements.begin(), random(elements.size()));
}
Graph::Graph(){};
......@@ -173,7 +182,6 @@ Graph::Graph(const string &filename) {
Graph *Graph::GenerateRandomGraph(int n_states, int n_transitions,
int n_labels) {
srand(time(nullptr)); // seed the random number generator
Graph *graph = new Graph();
// create states
......@@ -236,7 +244,7 @@ Graph *Graph::GenerateRandomGraph(int n_states, int n_transitions,
// add additional transitions randomly
while (counter_transitions < n_transitions) {
// choose a random source node
int source_id = rand() % n_states;
int source_id = random(n_states);
Node *source = graph->get_state(source_id);
// Ensure the chosen transition label is unique for the current node
......@@ -249,7 +257,7 @@ Graph *Graph::GenerateRandomGraph(int n_states, int n_transitions,
used_labels[source].insert(label);
// Choose a random target node
int target_id = rand() % n_states;
int target_id = random(n_states);
// Add the transition
graph->add_successor(source_id, target_id, label);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment