美文网首页
C# 随机数生成

C# 随机数生成

作者: Rinaloving | 来源:发表于2023-01-29 18:00 被阅读0次

生成随机数

1. 代码

public static string  GetRandom(int codeLenth)
{
            while (true)
            {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < codeLenth; i++)
                {
                    if (i == 0)
                        sb.Append(new Random().Next(9) + 1); // first field will not start with 0.
                    else
                        sb.Append(new Random().Next(10));
                }
                return sb.ToString();
            }
 }

2. 用法

GetRandom(10) // 长度

相关文章

网友评论

      本文标题:C# 随机数生成

      本文链接:https://www.haomeiwen.com/subject/siyphdtx.html