Developer Tools for Random Data Generation // v2.6.1
root@generate-random:~/phone-numbers/cpp$ _

Generate Random Phone Numbers in C++

Complete code tutorial with examples and best practices

[ Code Example - Quick Summary ]

Language: C++

What: Generate random phone numbers in C++ using std::random and string formatting. Covers US format, international format, and validation.

Try it: Use our interactive Phone-numbers generator or integrate this code into your C++ application.

Generate random phone numbers in C++ using std::random and string formatting. Covers US format, international format, and validation. Looking for other languages? Check our code examples in PHP , JavaScript , Python , Java , C# , Ruby and Go or use our interactive web generator.

C++ Code Example

#include <iostream>
#include <random>
#include <sstream>
#include <iomanip>
#include <vector>
#include <regex>

class PhoneNumberGenerator {
private:
    std::mt19937 rng;
    std::uniform_int_distribution<int> areaCodeDist;
    std::uniform_int_distribution<int> exchangeDist;
    std::uniform_int_distribution<int> lineNumberDist;
    std::uniform_int_distribution<int> digitDist;

public:
    PhoneNumberGenerator()
        : rng(std::random_device{}()),
          areaCodeDist(200, 999),
          exchangeDist(200, 999),
          lineNumberDist(1000, 9999),
          digitDist(0, 9) {}

    // Generate random US phone number (xxx) xxx-xxxx
    std::string randomUsPhoneNumber() {
        std::ostringstream oss;
        oss << "(" << std::setw(3) << std::setfill('0') << areaCodeDist(rng) << ") "
            << std::setw(3) << std::setfill('0') << exchangeDist(rng) << "-"
            << std::setw(4) << std::setfill('0') << lineNumberDist(rng);
        return oss.str();
    }

    // Generate random phone number with country code
    std::string randomInternationalPhone(const std::string& countryCode = "+1") {
        std::ostringstream oss;
        oss << countryCode << " (" << std::setw(3) << std::setfill('0') << areaCodeDist(rng) << ") "
            << std::setw(3) << std::setfill('0') << exchangeDist(rng) << "-"
            << std::setw(4) << std::setfill('0') << lineNumberDist(rng);
        return oss.str();
    }

    // Generate phone number without formatting
    std::string randomPhoneDigits(int length = 10) {
        std::ostringstream oss;
        for (int i = 0; i < length; ++i) {
            oss << digitDist(rng);
        }
        return oss.str();
    }

    // Validate US phone number format
    bool isValidUsPhone(const std::string& phone) {
        std::regex pattern(R"(^\(\d{3}\) \d{3}-\d{4}$)");
        return std::regex_match(phone, pattern);
    }

    // Generate multiple phone numbers
    std::vector<std::string> generatePhoneNumbers(int count = 10) {
        std::vector<std::string> phones;
        phones.reserve(count);
        for (int i = 0; i < count; ++i) {
            phones.push_back(randomUsPhoneNumber());
        }
        return phones;
    }
};

int main() {
    PhoneNumberGenerator generator;

    std::cout << "US Phone: " << generator.randomUsPhoneNumber() << std::endl;
    std::cout << "International: " << generator.randomInternationalPhone("+44") << std::endl;
    std::cout << "Digits only: " << generator.randomPhoneDigits(10) << std::endl;
    std::cout << "Valid: " << (generator.isValidUsPhone("(555) 123-4567") ? "true" : "false") << std::endl;

    auto batch = generator.generatePhoneNumbers(3);
    std::cout << "Batch: ";
    for (size_t i = 0; i < batch.size(); ++i) {
        std::cout << batch[i];
        if (i < batch.size() - 1) std::cout << ", ";
    }
    std::cout << std::endl;

    return 0;
}

[EXPLANATION]

C++ uses std::uniform_int_distribution for generating area codes (200-999), exchanges (200-999), and line numbers (1000-9999). std::ostringstream with std::setw() and std::setfill() formats with leading zeros. std::regex_match() validates patterns. std::mt19937 provides high-quality random generation. std::vector stores batch results with reserve() for efficiency. Raw string literal R"(...)" simplifies regex patterns.

Expected Output

US Phone: (555) 234-5678
International: +44 (555) 234-5678
Digits only: 5551234567
Valid: true
Batch: (234) 567-8901, (345) 678-9012, (456) 789-0123

Common Use Cases

  • Generate phone numbers for C++ application testing
  • Create contact data for embedded systems testing
  • Populate test databases from C++ applications
  • Test phone validation in telecommunications software
  • Generate mock data for VoIP application testing

Important Notes

  • std::setw(3) and std::setfill('0') format with leading zeros
  • std::mt19937 is the Mersenne Twister random number generator
  • std::regex requires C++11 or later
  • Raw string literals R"(...)" avoid escaping backslashes in regex
  • vector::reserve() pre-allocates memory for better performance

Try Our Interactive Generator

Don't want to write code? Use our free web-based Phone-numbers generator with instant results.

TRY PHONE-NUMBERS GENERATOR →