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

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

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,641 questions

55,376 answers

573 users

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

1 Answer

0 votes
using System;
using System.Collections.Generic;

class DistinctDigitGenerator
{
    // ------------------------------------------------------------
    // Function: generate_number
    // Purpose: Create one 3-digit integer with distinct digits,
    //          drawn from a shared pool of available digits.
    //          The first digit is never zero, so the result
    //          is always a true 3-digit integer.
    // ------------------------------------------------------------
    static int generate_number(List<char> available_digits, int length = 3)
    {
        Random rng = new Random();

        // Choose the first digit from the pool, excluding '0'
        List<char> non_zero_digits = new List<char>();
        foreach (char d in available_digits)
        {
            if (d != '0')
                non_zero_digits.Add(d);
        }

        char first_digit = non_zero_digits[rng.Next(non_zero_digits.Count)];
        available_digits.Remove(first_digit);

        // Choose the remaining digits from the updated pool
        List<char> remaining = new List<char>();
        for (int i = 1; i < length; i++)
        {
            char d = available_digits[rng.Next(available_digits.Count)];
            remaining.Add(d);
            available_digits.Remove(d);
        }

        // Build the number as a string, then convert to int
        string digits_str = first_digit + string.Join("", remaining);
        return int.Parse(digits_str);
    }


    // ------------------------------------------------------------
    // Function: generate_three_distinct_digit_numbers
    // Purpose: Produce three integers, each with distinct digits,
    //          and no digit repeated across all three numbers.
    //          All numbers are guaranteed to be 3 digits long.
    // ------------------------------------------------------------
    static Tuple<int, int, int> generate_three_distinct_digit_numbers()
    {
        // Start with all digits 0–9 as strings
        List<char> digits = new List<char>();
        for (int i = 0; i < 10; i++)
            digits.Add((char)('0' + i));

        // Generate three numbers, each 3 digits long
        int n1 = generate_number(digits);
        int n2 = generate_number(digits);
        int n3 = generate_number(digits);

        return Tuple.Create(n1, n2, n3);
    }


    // ------------------------------------------------------------
    // Main execution
    // ------------------------------------------------------------
    static void Main()
    {
        // Run the generator 5 times
        for (int i = 0; i < 5; i++)
        {
            var result = generate_three_distinct_digit_numbers();
            Console.WriteLine($"({result.Item1}, {result.Item2}, {result.Item3})");
        }
    }
}



/*
run:

(634, 270, 958)
(637, 890, 541)
(273, 514, 896)
(641, 570, 289)
(763, 198, 542)

*/

 



answered Jul 17 by avibootz

Related questions

...