C# 方法在应返回唯一值时未返回唯一值
我有两种方法,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果没有看到 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.
我高度怀疑您的 randomIndex 方法会产生以下效果:
上述代码的问题是,当您在不同时间运行它时,它会返回不同的值。但是,如果您像下面这样运行它,它几乎总是返回相同的值:
这是因为随机数生成器使用当前时间作为其种子。 2 个随机化器同时创建,将始终返回相同的值。正确的方法是:
或者,您可以使成员静态。
I highly suspect your randomIndex method does something to the following effect:
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:
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:
Alternatively, you can make the members static.