测试快速在循环 连续生成随机码会导致部分重复,如下 

List<string> randoms = new List<string>();
for (int i = 0; i < 100; i++)
{
           string random = new System.Random().Next(999).ToString("000");;
           randoms.Add(random);
}

 可以使用以下方法保证每次取到唯一

namespace Utility
{
private static readonly Random random = new Random(); private static readonly object _lock = new object(); public static string GetRandom(int min, int max) { lock (_lock) { return random.Next(min, max).ToString("000"); } } } 引用 string num=Utility.RandomNumber(0, 999)

 

相关文章:

  • 2021-04-07
  • 2021-11-30
  • 2021-12-28
  • 2022-12-23
  • 2021-08-03
  • 2021-07-23
  • 2021-09-12
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-09-28
  • 2022-12-23
  • 2021-05-19
  • 2021-11-30
相关资源
相似解决方案