为什么这个方法每次都返回相同的随机字符串?

编程入门 行业动态 更新时间:2024-10-25 12:19:58
本文介绍了为什么这个方法每次都返回相同的随机字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要创建一组独特的线条来测试我正在处理的另一个项目.

I need to create a block of unique lines to test a different project I am working on.

所以我创建了一个简单的程序来生成一个长度为 X 的随机字符串.

So I created a simple program to generate a random string of X length.

问题是,如果我调用它一次,我会得到一个随机字符串,如果我再次调用它(例如在 for 循环中),我会在整个循环执行过程中得到相同的字符串.

The issue is that if I call it once, I get a random string, if I call it again (in a for loop for example) I get the same string for the entire execution of the loop.

我有一种感觉,它正在被缓存或其他东西,但我不知道 这样做了,我只是在这一点上感到困惑.

I have a feeling that it's being cached or something but I didn't know did that and I am just confused at this point.

调用代码:

StreamWriter SW = new StreamWriter("c:\test.txt"); int x = 100; while (x >0) { SW.WriteLine(RandomString(20)); x--; }

方法如下:

private static string RandomString(int Length) { StringBuilder sb = new StringBuilder(); Random randomNumber = new Random(); for (int i = 0; i <= Length; ++i) { int x = randomNumber.Next(65, 122); sb.Append(Convert.ToChar(x)); } return sb.ToString(); }

这里是输出:

"VEWMCQ`Fw]TvSFQawYnoB VEWMCQ`Fw]TvSFQawYnoB VEWMCQ`Fw]TvSFQawYnoB VEWMCQ`Fw]TvSFQawYnoB VEWMCQ`Fw]TvSFQawYnoB VEWMCQ`Fw]TvSFQawYnoB .................. VEWMCQ`Fw]TvSFQawYnoB VEWMCQ`Fw]TvSFQawYnoB VEWMCQ`Fw]TvSFQawYnoB VEWMCQ`Fw]TvSFQawYnoB VEWMCQ`Fw]TvSFQawYnoB"

那么是什么让我认为 Random.next() 总是会返回一个新的随机数?

So what gives i thought Random.next() would always return a new random number?

推荐答案

您创建的 Random 实例时间太近了.每个实例都使用系统时钟进行初始化,由于时钟没有改变,您一遍又一遍地得到相同的随机数序列.

You are creating the Random instances too close in time. Each instance is initialised using the system clock, and as the clock haven't changed you get the same sequence of random numbers over and over.

创建 Random 类的单个实例并反复使用它.

Create a single instance of the Random class and use it over and over.

使用 using 关键字,以便在使用完 StreamWriter 后关闭并释放它.如果使用 for 关键字,循环的代码更容易识别.

Use the using keyword so that the StreamWriter is closed and disposed when you are done with it. The code for a loop is easier to recognise if you use the for keyword.

using (StreamWriter SW = new StreamWriter("c:\test.txt")) { Random rnd = new Random(); for (int x = 100; x > 0; x--) { SW.WriteLine(RandomString(rnd, 20)); } }

该方法将 Random 对象作为参数.

The method takes the Random object as a parameter.

此外,使用长度以正确的容量初始化 StringBuilder,这样它就不必在循环期间重新分配.使用 <运算符而不是循环中的 <=,否则您将创建一个比 length 参数指定的长度长一个字符的字符串.

Also, use the length to initialise the StringBuilder with the correct capacity, so that it doesn't have to reallocate during the loop. Use the < operator instead of <= in the loop, otherwise you will create a string that is one character longer than the length parameter specifies.

private static string RandomString(Random rnd, int length) { StringBuilder sb = new StringBuilder(length); for (int i = 0; i < length; i++) { int x = rnd.Next(65, 122); sb.Append((char)x); } return sb.ToString(); }

更多推荐

为什么这个方法每次都返回相同的随机字符串?

本文发布于:2023-11-06 09:53:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563368.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:每次都   字符串   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!