Skip to main content

Overview

The GenerateRandomNames class generates random lowercase alphabetic names that serve as unique identifiers for players in the Firebase database. This approach provides anonymous player tracking without requiring user authentication.

Class Definition

namespace vbEngC
{
    public class GenerateRandomNames
    {
        public string GenerateRandomName(Random rand)
        {
            // Implementation
        }
    }
}

Methods

GenerateRandomName

Generates a random name composed of lowercase letters with a length between 4 and 9 characters.

Method Signature

public string GenerateRandomName(Random rand)

Parameters

rand
Random
required
An instance of the System.Random class used for generating random values. This parameter allows for better control over randomization and testing.

Returns

string
string
A randomly generated name consisting of 4 to 9 lowercase letters (e.g., “abxkfm”, “wzjp”, “mkloptqr”).

Implementation Details

The method follows these steps to generate a random name:
public string GenerateRandomName(Random rand)
{
    // Longitud del nombre aleatorio
    int nameLength = rand.Next(4, 10); // Por ejemplo, de 4 a 9 caracteres

    // Array de caracteres permitidos para el nombre
    char[] allowedChars = "abcdefghijklmnopqrstuvwxyz".ToCharArray();

    // Construye el nombre aleatorio
    char[] name = new char[nameLength];
    for (int i = 0; i < nameLength; i++)
    {
        name[i] = allowedChars[rand.Next(0, allowedChars.Length)];
    }

    // Convierte el array de caracteres en una cadena
    return new string(name);
}

Algorithm Steps

  1. Determine length: Randomly selects a name length between 4 and 9 characters
  2. Define character set: Uses all 26 lowercase letters (a-z)
  3. Build name: Iteratively selects random characters from the allowed set
  4. Return result: Converts the character array to a string

Usage Examples

Basic Usage

// Create an instance of GenerateRandomNames
GenerateRandomNames names = new GenerateRandomNames();

// Create a Random instance
Random rand = new Random();

// Generate a random name
string randomName = names.GenerateRandomName(rand);

Console.WriteLine($"Generated name: {randomName}");
// Output example: "Generated name: fhjkwm"

Integration with Player System

The class is used during player initialization to create unique database keys:
// Class to generate names for the user
GenerateRandomNames names = new GenerateRandomNames();
Random rand = new Random();

// Generate a random name
string randomName = names.GenerateRandomName(rand);

// Use the random name as a database key
var setter = client.Set("PlayerIpList/" + randomName, ply);

Generating Multiple Names

GenerateRandomNames generator = new GenerateRandomNames();
Random rand = new Random();

// Generate multiple unique identifiers
List<string> playerNames = new List<string>();
for (int i = 0; i < 10; i++)
{
    playerNames.Add(generator.GenerateRandomName(rand));
}

// Example output: ["mkwp", "zxcvbnm", "qwerty", "asdfgh", ...]

Technical Specifications

Name Length
int
Range: 4 to 9 charactersDistribution: Uniform random distribution
Character Set
char[]
Allowed characters: Lowercase letters a-z (26 characters)Character encoding: ASCII/UTF-8 compatible
Uniqueness
probability
Collision probability: Low but not zeroWith 26 characters and lengths 4-9, there are approximately 321 million possible combinations, making collisions rare in typical use cases.

Design Considerations

Why Random Names?

Random names provide several advantages:
  • Anonymous tracking: No personal information required
  • Simple implementation: No authentication system needed
  • Unique identifiers: Sufficient uniqueness for small to medium player bases
  • Database keys: Clean, consistent key format for Firebase

Collision Handling

The current implementation does not check for name collisions in the database. In a production environment with many concurrent users, consider implementing collision detection and retry logic.

Potential Improvements

// Enhanced version with collision checking
public async Task<string> GenerateUniqueNameAsync(Random rand, IFirebaseClient client)
{
    string name;
    bool exists;
    
    do
    {
        name = GenerateRandomName(rand);
        var result = await client.GetAsync("PlayerIpList/" + name);
        exists = result.Body != "null";
    } while (exists);
    
    return name;
}

Character Distribution

The method provides equal probability for each character position:
// Each letter has a 1/26 chance of being selected
// Name length distribution:
// - 4 characters: ~16.7% chance
// - 5 characters: ~16.7% chance
// - 6 characters: ~16.7% chance
// - 7 characters: ~16.7% chance
// - 8 characters: ~16.7% chance
// - 9 characters: ~16.7% chance
  • Player - Uses random names as database keys
  • Forms - Implements name generation during player initialization
  • IP Address - Works alongside random names for player identification

Testing Considerations

When testing, you can control the randomization by using a seeded Random instance:
// Reproducible random names for testing
Random seededRandom = new Random(42);
GenerateRandomNames generator = new GenerateRandomNames();

string name1 = generator.GenerateRandomName(seededRandom);
string name2 = generator.GenerateRandomName(seededRandom);

// These will be consistent across test runs with the same seed

Build docs developers (and LLMs) love