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

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

问题描述

我需要创建独特的行块,以测试不同的项目我的工作。

所以,我创建了一个简单的程序来生成一个随机序列X的长度。

问题是,如果我一旦称呼它,我得到一个随机字符串,如果我再次调用它(在for循环为例)我得到同样的字符串循环的整个执行。

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

调用code:

  StreamWriter的SW =新的StreamWriter(C:\\的test.txt);
    INT X = 100;
    而(X 0)
    {
        SW.WriteLine(RandomString(20));
        x--;
    }
 

这里是方法:

 私有静态字符串RandomString(INT长度)
{
    StringBuilder的SB =新的StringBuilder();
    随机randomNumber =新的随机();

    的for(int i = 0; I< =长度; ++ I)
    {
        INT X = randomNumber.Next(65,122);
        sb.Append(Convert.ToChar(X));
    }
    返回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()总是会返回一个新的随机数?

解决方案

您正在创建的随机实例太近的时间。各实例使用的系统时钟初始化,并作为时钟没有改变你反复获得的随机数相同的序列

创建随机类的一个实例,并一遍又一遍地用它。

使用了使用关键字,使的StreamWriter 关闭和处置,当你用它做。在$ C $下一个循环是比较容易,如果你使用关键字识别。

 使用(StreamWriter的SW =新的StreamWriter(C:\\的test.txt)){
   随机RND =新的随机();
   为(中间体X = 100,X大于0; x--){
      SW.WriteLine(RandomString(RND,20));
   }
}
 

这个方法以随机对象作为参数。<​​/ P>

此外,使用长度来初始化StringBuilder的与正确的容量,因此,它不具有循环期间重新分配。使用的&lt;操盘手&LT; =在循环,否则你将创建一个字符串,它是一个字符长度超过了长度参数指定

 私有静态字符串RandomString(随机RND,INT的长度){
   StringBuilder的SB =新的StringBuilder(长度);
   的for(int i = 0; I&LT;长度;我++){
      INT X = rnd.Next(65,122);
      sb.Append((char)的X);
   }
   返回sb.ToString();
}
 

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

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

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 .net did that and I am just confused at this point.

calling code:

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

here is the method:

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();        
}

and here is the output:

"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"

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

解决方案

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.

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

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));
   }
}

The method takes the Random object as a parameter.

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天全站免登陆