C# 方法在应返回唯一值时未返回唯一值

发布于 2024-10-20 11:39:34 字数 1335 浏览 1 评论 0原文

我有两种方法,generateNounPhrase() 和generateVerbPhrase()。 VerbPhrase 有一半时间会调用 NounPhrase,它的输出应该具有以下效果:

空地重新激活这个金字塔

(粗体表示在逻辑上调用generateNounPhrase()的位置)。然而,真正的输出是以下形式:

空地重新激活空地

起初我以为我的 randomIndex 方法没有按我的预期工作,但如果我再次运行这两个方法,我确实得到了不同的名词短语,但它们在句子的开头和结尾并不像应有的那样唯一。

知道我做错了什么才能得到一种方法来显示相同​​的结果吗?

private string generateNounPhrase()
{
    string nounPhraseString = "";
    nounPhraseString = nounMarkersStringList[randomIndex(0,nounMarkersStringList.Count-1)];
    if (included(1, 4, 2) == true)
    {
        nounPhraseString += " " + adjectivesStringList[randomIndex(0, adjectivesStringList.Count - 1)];
    }
    nounPhraseString += " " + nounsStringList[randomIndex(0, nounsStringList.Count - 1)];
    return nounPhraseString;
}

private string generateVerbPhrase()
{
    string verbPhraseString = "";
    if (included(1, 4, 2) == true)
    {
        verbPhraseString = intransitiveVerbsStringList[randomIndex(0, intransitiveVerbsStringList.Count - 1)];
    }
    else
    {
        verbPhraseString = transitiveVerbsStringList[randomIndex(0, transitiveVerbsStringList.Count - 1)] + " " + generateNounPhrase();
    }
    return verbPhraseString;
}

I have two methods, generateNounPhrase() and generateVerbPhrase(). VerbPhrase will call on NounPhrase half the time and it's output the output should be something to the effect of:

the empty lot re-animates this pyramid

(bold indicating where generateNounPhrase() is logically called). The true output however is in the form of:

the empty lot re-animates the empty lot

At first I thought my randomIndex method wasn't working as I had intended, but if I run the two methods again I do get different noun phrases but they are not unique at the beginning and end of the sentence as they should be.

Any idea what I am doing wrong in order to get one method to show the same result?

private string generateNounPhrase()
{
    string nounPhraseString = "";
    nounPhraseString = nounMarkersStringList[randomIndex(0,nounMarkersStringList.Count-1)];
    if (included(1, 4, 2) == true)
    {
        nounPhraseString += " " + adjectivesStringList[randomIndex(0, adjectivesStringList.Count - 1)];
    }
    nounPhraseString += " " + nounsStringList[randomIndex(0, nounsStringList.Count - 1)];
    return nounPhraseString;
}

private string generateVerbPhrase()
{
    string verbPhraseString = "";
    if (included(1, 4, 2) == true)
    {
        verbPhraseString = intransitiveVerbsStringList[randomIndex(0, intransitiveVerbsStringList.Count - 1)];
    }
    else
    {
        verbPhraseString = transitiveVerbsStringList[randomIndex(0, transitiveVerbsStringList.Count - 1)] + " " + generateNounPhrase();
    }
    return verbPhraseString;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谜泪 2024-10-27 11:39:34

如果没有看到 randomIndex 的代码,我无法确定,但看起来您每次调用 randomIndex 时可能都会创建 Random 类的新实例。如果您在很短的时间内执行此操作两次,就像您在这里一样,它将两次为随机数生成器播种相同的值(因为它是用当前时间播种的),并且您将获得相同的“随机”数两次都回来了。

相反,您应该对所有 randomIndex 调用使用单个 Random 实例。

Without seeing the code for randomIndex, I can't be certain, but it seems like you are probably creating a new instance of the Random class each time you call randomIndex. If you do this twice in a very short time, as you would be here, it will seed the random number generator with the same value both times (because it is seeded with the current time) and you will get the same 'random' number returned both times.

Instead, you should use a single instance of Random for all randomIndex calls.

别把无礼当个性 2024-10-27 11:39:34

我高度怀疑您的 randomIndex 方法会产生以下效果:

Random r = new Random();
return r.Next (0, max);

上述代码的问题是,当您在不同时间运行它时,它会返回不同的值。但是,如果您像下面这样运行它,它几乎总是返回相同的值:

Console.WriteLine ("{0} == {1}?", randomIndex(0,10), randomIndex(0,10));

这是因为随机数生成器使用当前时间作为其种子。 2 个随机化器同时创建,将始终返回相同的值。正确的方法是:

class MyRand  // Assuming MyRand is the class name
{
   private Random r = new Random();
   public int GetRand(int min, int max)
   {
       return r.Next(min, max);
   }
}

或者,您可以使成员静态。

I highly suspect your randomIndex method does something to the following effect:

Random r = new Random();
return r.Next (0, max);

The problem with the above code is that when you run it at different time, it returns a different value. But if you run it like the following, it almost always returns the same value:

Console.WriteLine ("{0} == {1}?", randomIndex(0,10), randomIndex(0,10));

This is because the randomizer uses the current time as its seed. 2 randomizer created at the same time, will always return the same value. The right way to do it would be:

class MyRand  // Assuming MyRand is the class name
{
   private Random r = new Random();
   public int GetRand(int min, int max)
   {
       return r.Next(min, max);
   }
}

Alternatively, you can make the members static.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文