为什么这个方法每次都返回相同的随机字符串? [英] why does this method return the same random string each time?

查看:21
本文介绍了为什么这个方法每次都返回相同的随机字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

I have a feeling that it's being cached or something but I didn't know .net 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();        
}

这篇关于为什么这个方法每次都返回相同的随机字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆