Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

How to create a list of random file names in C++

1 Answer

0 votes
#include <iostream>
#include <vector>
#include <string>
#include <random> // random_device // mt19937 // uniform_int_distribution

// Function to generate a random string of given length
std::string generateRandomString(size_t length) {
    const std::string characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    std::random_device rd;
    std::mt19937 generator(rd());
    std::uniform_int_distribution<> distribution(0, characters.size() - 1);

    std::string randomString;
    for (size_t i = 0; i < length; i++) {
        randomString += characters[distribution(generator)];
    }

    return randomString;
}

// Function to create a list of random file names
std::vector<std::string> createRandomFileNames(size_t totalFiles, size_t nameLength, std::string extension) {
    std::vector<std::string> fileNames;
    for (size_t i = 0; i < totalFiles; i++) {
        fileNames.push_back(generateRandomString(nameLength) + extension);
    }
    return fileNames;
}

int main() {
    size_t totalFiles = 5; // Number of file names to generate
    size_t nameLength = 10; // Length of each file name (excluding extension)

    std::vector<std::string> fileNames = createRandomFileNames(totalFiles, nameLength, ".txt");

    for (const auto& fileName : fileNames) {
        std::cout << fileName << std::endl;
    }
}

   
   
/*
run:
   
8LAiVJStVI.txt
2AesuTM9oJ.txt
oLo6x1GpSO.txt
wMvxNKL3OY.txt
m9AwBw2Fx0.txt
   
*/

 



answered Apr 18, 2025 by avibootz
edited Apr 18, 2025 by avibootz
...