Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve random nick generation #3834

Merged
merged 3 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions Client/core/CNickGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
*****************************************************************************/

#include "StdInc.h"
#include "time.h"
#include <random>

// These words are of a maximum length of 10 characters, capitalized, and stripped of whitespace
const char* const CNickGen::m_szAdjectives[] = {
const char* const szAdjectives[] = {
"Aback", "Abaft", "Abandoned", "Abashed", "Aberrant", "Abhorrent", "Abiding", "Abject", "Ablaze", "Able", "Abnormal",
"Aboard", "Aboriginal", "Abortive", "Abounding", "Abrasive", "Abrupt", "Absent", "Absorbed", "Absorbing", "Abstracted", "Absurd",
"Abundant", "Abusive", "Acceptable", "Accessible", "Accidental", "Accurate", "Acid", "Acidic", "Acoustic", "Acrid", "Actually",
Expand Down Expand Up @@ -109,7 +109,7 @@ const char* const CNickGen::m_szAdjectives[] = {
"Worried", "Worthless", "Wrathful", "Wretched", "Wrong", "Wry",
};

const char* const CNickGen::m_szNouns[] = {
const char* const szNouns[] = {
"Aardvark", "Buffalo", "Alligator", "Ant", "Anteater", "Antelope", "Ape", "Armadillo", "Donkey", "Baboon", "Badger",
"Barracuda", "Bat", "Bear", "Beaver", "Bee", "Bison", "Boar", "Bush", "Butterfly", "Camel", "Calf",
"Cat", "Kitten", "Cattle", "Chamois", "Cheetah", "Chicken", "Chick", "Chimpanzee", "Infant", "Empress", "Troop",
Expand Down Expand Up @@ -196,10 +196,18 @@ const char* const CNickGen::m_szNouns[] = {
"Vampire", "Parasite", "Tramp", "Bum", "Hobo", "Hitchhiker", "Deadbeat", "Acrobat",
};

const int numAdjectives = std::size(szAdjectives);
const int numNouns = std::size(szNouns);
const int maxNum = static_cast<int>(RAND_MAX);
TheNormalnij marked this conversation as resolved.
Show resolved Hide resolved

SString CNickGen::GetRandomNickname()
{
srand((unsigned int)time(NULL));
int iAdjective = rand() % NICKGEN_NUM_ADJECTIVES;
int iNoun = rand() % NICKGEN_NUM_NOUNS;
return SString("%s%s%i", m_szAdjectives[iAdjective], m_szNouns[iNoun], rand() % 100);
std::random_device rd;
std::mt19937 gen(rd());

std::uniform_int_distribution<int> adjectiveDist(0, numAdjectives - 1);
std::uniform_int_distribution<int> nounDist(0, numNouns - 1);
std::uniform_int_distribution<int> numDist(0, maxNum);

return SString("%s%s%i", szAdjectives[adjectiveDist(gen)], szNouns[nounDist(gen)], numDist(gen));
}
5 changes: 0 additions & 5 deletions Client/core/CNickGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,8 @@

#pragma once

#define NICKGEN_NUM_ADJECTIVES 1048
#define NICKGEN_NUM_NOUNS 934

class CNickGen
{
public:
static const char* const m_szAdjectives[NICKGEN_NUM_ADJECTIVES];
static const char* const m_szNouns[NICKGEN_NUM_NOUNS];
static SString GetRandomNickname();
};
Loading