How to generate a random 10 digit number in C#?

后端 未结 13 1912
清歌不尽
清歌不尽 2020-12-15 18:00

I\'m using C# and I need to generate a random 10 digit number. So far, I\'ve only had luck finding examples indicating min maximum value. How would i go about generating a r

13条回答
  •  余生分开走
    2020-12-15 18:41

    Here is a simple solution using format string:

    string r = $"{random.Next(100000):00000}{random.Next(100000):00000}";
    

    Random 10 digit number (with possible leading zeros) is produced as union of two random 5 digit numbers. Format string "00000" means leading zeros will be appended if number is shorter than 5 digits (e.g. 1 will be formatted as "00001").

    For more about the "0" custom format specifier please see the documentation.

提交回复
热议问题