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

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

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

Disclosure: My content contains affiliate links.

43,239 questions

56,142 answers

573 users

How to generate 3 no-digit repeated random integers, each with distinct digits in C++

1 Answer

0 votes
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <random>

// ------------------------------------------------------------
// Function: generateNumber
// Purpose: Create one 3-digit string with distinct digits,
//          drawn from a shared pool. Leading zeros preserved.
// ------------------------------------------------------------
std::string generateNumber(std::vector<char>& availableDigits, int length = 3) {
    static std::random_device rd;
    static std::mt19937 gen(rd());

    // Shuffle available digits
    std::shuffle(availableDigits.begin(), availableDigits.end(), gen);

    // Pick first 'length' digits
    std::string chosen(availableDigits.begin(), availableDigits.begin() + length);

    // Remove chosen digits from pool
    for (char d : chosen) {
        availableDigits.erase(std::remove(availableDigits.begin(), availableDigits.end(), d),
                              availableDigits.end());
    }

    return chosen; // keep as string to preserve leading zeros
}

// ------------------------------------------------------------
// Function: generateThreeDistinctDigitNumbers
// Purpose: Produce three 3-digit numbers with distinct digits
//          and no digit repeated across all numbers.
// ------------------------------------------------------------
std::tuple<std::string, std::string, std::string> generateThreeDistinctDigitNumbers() {
    std::vector<char> digits = {'0','1','2','3','4','5','6','7','8','9'};

    std::string n1 = generateNumber(digits);
    std::string n2 = generateNumber(digits);
    std::string n3 = generateNumber(digits);

    return {n1, n2, n3};
}

// ------------------------------------------------------------
// Main execution
// ------------------------------------------------------------
int main() {
    // Run the generator 5 times
    for (int i = 0; i < 5; ++i) {
        auto [a, b, c] = generateThreeDistinctDigitNumbers();
        std::cout << a << ", " << b << ", " << c << "\n";
    }
}


/*
run:

491, 026, 753
729, 436, 105
614, 529, 830
137, 580, 469
250, 639, 814

*/

 



answered Jul 17 by avibootz

Related questions

...