由于您明确要求递归,因此这是一个递归解决方案。不过,它\xe2\x80\x99s 非常慢。
\n\nstatic string allowedCharacters = "abcdefghijklmnopqrstuvwxyz0123456789";\nstatic Random rnd = new Random();\nstatic string randomString(int length)\n{\n if (length == 0)\n return "";\n return allowedCharacters[rnd.Next(0, allowedCharacters.Length)]\n + randomString(length - 1); // This is the recursive call.\n}\nRun Code Online (Sandbox Code Playgroud)\n\n现在您可以使用它来生成随机长度的字符串:
\n\n// Outputs a random string of a length between 5 and 49 characters\nConsole.WriteLine(randomString(rnd.Next(5, 50)));\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
666 次 |
| 最近记录: |