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,401 questions

56,365 answers

573 users

How to generate an 8 digits random number in C#

2 Answers

0 votes
using System;

public class RandomNumberProgram 
{
    public static void Main(string[] args) 
    {
        const int N = 20; 
        Random rnd = new Random();

        for (int i = 0; i < N; i++) 
        {
            // Upper bound is exclusive, so 100000000 allows numbers up to 99999999
            Console.WriteLine(rnd.Next(10000000, 100000000));
        }
    }
}



/*
run:

95900969
23746410
68581316
25078965
18813602
22889103
15604020
77516928
10934765
42841347
11133774
72889579
36646464
80579430
61833656
45686709
23346769
36332235
86049073
60917518

*/

 



answered Apr 27, 2016 by avibootz
edited 3 days ago by avibootz
0 votes
using System;

namespace RandomNumberProgram
{
    public static class RandomGenerator
    {
        // Class-level instance shared across calls to avoid duplicate seed issues.
        // Marked ThreadStatic so each thread gets its own instance, making it thread-safe.
        [ThreadStatic]
        private static Random _rng;

        // Lazy initialization property for thread-safe access
        private static Random Rng => _rng ??= new Random();

        private const int Min8DigitInteger = 10000000;
        private const int Max8DigitIntegerInclusive = 99999999;

        /// <summary>
        /// Generates a random 8-digit integer between 10,000,000 and 99,999,999 (inclusive).
        /// </summary>
        public static int Generate8DigitNumber()
        {
            // Random.Next(min, max) excludes the max bound, so 100000000 is used
            return Rng.Next(Min8DigitInteger, Max8DigitIntegerInclusive + 1);
        }

        /// <summary>
        /// Generates an 8-character string preserving leading zeros (00000000 to 99999999).
        /// </summary>
        public static string Generate8DigitCode()
        {
            int rawValue = Rng.Next(0, 100000000);

            return rawValue.ToString("D8");
        }
    }

    public class Program
    {
        public static void Main(string[] args)
        {
            int randomNumber = RandomGenerator.Generate8DigitNumber();
            Console.WriteLine("Generated 8-digit integer: " + randomNumber);

            string randomCode = RandomGenerator.Generate8DigitCode();
            Console.WriteLine("Generated 8-digit string code: " + randomCode);
        }
    }
}



/*
run:

Generated 8-digit integer: 29893146
Generated 8-digit string code: 27618984

*/

 



answered 3 days ago by avibootz
...