I have that funtion for generate sequence to the style 'hqhzaw', 'ebcpm', 'qtch', etc. (only letters) and according to 'lenght'

string random_letter(int length) {
	string s;

	for (int i = 0; i < length; i++) {
       		s += char(rand() % 26 + 97);
	}

	return s;
}

I want to generate, using this funtion or some other, array of letters and numbers to the style 'tn4v9k2e0', '8ezt0p3', 5cye7' according to 'lenght'.

Thanks.

Dani AI

Generated

started with an ASCII/rand() approach and suggested picking from an explicit character set. That is a simple and clear solution. For more predictable, higher-quality results in modern C++, prefer a C++11 random engine and a combined charset (digits + letters). This avoids common pitfalls such as reseeding on every call, modulo bias from rand() % n, and accidental huge allocations when a negative int is passed.

#include <random>
#include <string>

std::string random_alnum(std::size_t length) {
    static const char charset[] =
        "0123456789"
        "abcdefghijklmnopqrstuvwxyz"
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    const std::size_t charset_size = sizeof(charset) - 1; // exclude terminator
    thread_local static std::mt19937 rng(std::random_device{}());
    std::uniform_int_distribution<std::size_t> dist(0, charset_size - 1);

    std::string result;
    result.reserve(length);
    for (std::size_t i = 0; i < length; ++i)
        result += charset[dist(rng)];
    return result;
}

Notes and caveats: reserve capacity to avoid reallocations; use std::size_t for the length parameter or validate an int before converting to prevent huge allocations; thread_local + a single mt19937 instance avoids repeated seeding and is safe in multithreaded code. std::uniform_int_distribution removes the simple modulo bias present in rand() % n. For cryptographic or high-security tokens use a CSPRNG (platform APIs or libs like libsodium) instead of mt19937. See the C++ reference pages for details on std::mt19937, std::uniform_int_distribution and std::random_device.

This keeps 's character-set idea but adds safer, modern RNG practices and practical safeguards over the original rand()-based snippet.

Recommended Answers

All 2 Replies

instead of using the ascii codes which would get a bit more complicated when you add numbers because of the disconnect between the codes (since numbers are 48-57 and letters 97-122), why don't you try something like the following as a starting point:

string random_letter(int length) 
{
     const string values = "abcdefghijklmnopqrstuvwxyz";
     const int valuesSize = 26;
     string s;

     for (int i = 0; i < length; i++) 
     {
          s += values[rand() % valuesSize];
     }

     return s;
}

then you can add whatever characters you want (numbers), just a suggestion.

~J

Thank you. Works very well for me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.