问题描述
public float randomNumber(float x, float y, Random rnd)
{
//Return a random number between 0 - 1
}
所以我的问题是我需要随机数与 x 和 y 相关.如果我传入 x = 10, y = 5 并且当我第二次传入 x = 10, y = 5 时我得到 0.34567f,我再次需要 0.34567f 的结果.有人知道这样做的方法吗?
So my problem is I need the random number to be related to x and y. If I pass in x = 10, y = 5 and I get out 0.34567f when I pass in x = 10, y = 5 a second time I need a result of 0.34567f again. Does anyone know a way of doing this?
根据下面的回复,我意识到我遗漏了一些东西.我传入 Random 对象的原因是因为它已在其他地方预先播种.因此,如果我为种子为 50 的随机对象传入 x = 10, y = 5,然后用 0.34567f 对种子为 51 的随机对象执行相同的操作,结果应该会有所不同.
Based on the responses below I realize I left something out. The reason I pass in the Random object is because it has been pre-seeded elsewhere. So if I pass in x = 10, y = 5 for a random object with a seed of 50 and get out 0.34567f doing the same thing with a Random object with a seed of 51 should give me something different.
推荐答案
您可以将它们用作种子:
You can use them as a seed:
int seed =
BitConverter.ToInt32(BitConverter.GetBytes(
x * 17 + y
));
new Random(seed).NextDouble();
这篇关于生成与输入参数相关的随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!