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
*/