How to generate N random integers, each with distinct digits and exact length L in C#

2 Answers

0 votes
using System;
using System.Text;

class DistinctDigitRandomNumbers
{
    /*
        Generate a single random integer of length L whose digits are all distinct.
        The function returns the number as a string to preserve leading digits if needed.
    */
    static string generateDistinctDigitNumber(int L, Random rng)
    {
        // Digits 0–9 are available; we will pick L distinct digits.
        // Using an array to store the chosen digits.
        // The result string will be built step by step.

        bool[] used = new bool[10];
        StringBuilder result = new StringBuilder(L);
        int count = 0;

        while (count < L) {
            // Always generate digits '0'..'9' reliably
            char d = (char)(48 + rng.Next(10));

            // If digit not used yet, accept it.
            if (!used[d - '0']) {
                used[d - '0'] = true;
                result.Append(d);
                count++;
            }
        }

        return result.ToString();
    }

    /*
        Generate N random integers, each with distinct digits and length L.
        Prints them directly.
    */
    static void generateNumbers(int N, int L, Random rng)
    {
        for (int i = 0; i < N; i++) {
            string s = generateDistinctDigitNumber(L, rng);
            Console.WriteLine(s);
        }
    }

    static void Main()
    {
        int N = 10;   // how many numbers to generate
        int L = 6;    // length of each number

        // Random engine setup: seeded with current time.
        Random rng = new Random();

        // Generate the numbers
        Console.WriteLine("Generated " + N + " numbers of length " + L + " with distinct digits:");
        generateNumbers(N, L, rng);
    }
}



/*
run:

Generated 10 numbers of length 6 with distinct digits:
610837
406295
542896
421895
974053
027861
420869
152973
041852
613750

*/

 



answered Jul 16 by avibootz
0 votes
using System;
using System.Collections.Generic;

class Program
{
    static List<int> GenerateDistinctDigitNumbers(int N, int L)
    {
        HashSet<int> results = new HashSet<int>();   // store unique valid numbers
        int lower = (int)Math.Pow(10, L - 1);        // smallest L-digit number (e.g., 10000 for L=5)
        int upper = (int)Math.Pow(10, L) - 1;        // largest L-digit number (e.g., 99999 for L=5)

        Random rnd = new Random();                   // random number generator

        // keep generating until we have N distinct-digit numbers
        while (results.Count < N) {
            int num = rnd.Next(lower, upper + 1);    // generate random L-digit integer
            string s = num.ToString();               // convert number to string

            // check if all digits are unique
            HashSet<char> digitSet = new HashSet<char>(s);
            if (digitSet.Count == L) {
                results.Add(num);                    // add to set (ensures no duplicates)
            }
        }

        return new List<int>(results);               // convert set to list
    }

    static void Main()
    {
        var nums = GenerateDistinctDigitNumbers(10, 5);
        
        Console.WriteLine(string.Join(", ", nums));
    }
}



/*
run:

53084, 59370, 41538, 90573, 21674, 98416, 93841, 26450, 32780, 18093

*/

 



answered Jul 16 by avibootz

Related questions

...