随机数的行为怪异,不完全随机的随机数、不完全、怪异、行为

2023-09-05 04:01:22 作者:输的太彻底〞

在我的.NET游戏我rand函数被确定有多少伤害,淘汰的选手五个字符应该采取,但一日1似乎总是在刻度的底部,最后一个在顶部。所以在我的字[0] 的伤害很少超过最低兰特价值超过1以上,并为每个字符较高指数所采取的伤害只能从越往上规模随机的。

 公众诠释GetDamage(INT低,诠释高)
{
    随机R =新的随机();
    INT兰特= r.Next(低,高);
    返回兰特;
}
 

这是我用的是随机数发生器。然后,我更新离开这样的状况:

  INT伤害= GetDamage(3,10);
字符[目标]。健康=字符[目标]。健康 - 伤害;
 
财园趣谈 不完全新手体测攻略

在这个例子中伤害划分是这样的:

 数1:3-4
2号:4-6
3号:5-7
号码4:7-8
号码5:8-9
 

解决方案

您必须以其他方式重复使用相同的随机情况下,你不会得到真正的任意的值,因为它是与当前时间作为创建种子。如果你调用 GetDamage 速度非常快(如在一个循环),你将总是得到相同的值。

因此​​,无论使用字段/属性中的类 GetDamage 的或随机的实例传递给方法。

 专用随机_rnd =新的随机();
公众诠释GetDamage(INT低,诠释高)
{
    INT兰特= _rnd.Next(低,高);
    返回兰特;
}
 

MSDN

  

的随机数生成开始从种子值。如果同一   种子反复使用,则产生相同的一系列数字。一   的方式来产生不同的序列是使种子值   时间依赖性,由此产生不同的系列,每个新的   例如随机的。默认情况下,该参数的构造函数   Random类使用系统时钟产生的种子值,而   其参数的构造函数可以根据对一个Int32值   在当前时间的刻度数。但是,由于该时钟具有   有限的分辨率,使用无参数的构造函数来创建   在靠近继承不同的随机对象创建随机数   产生的随机数相同的序列发生器。 此   可避免通过创建一个单独的随机对象,而不是问题   多重的。

In my .NET game my rand function that are determining how much damage each out of the players five characters should take, however the 1st one always seems to be at the bottom of the scale and the last one at the top. So in my Character[0] the damage is rarely more than 1 more than the minimum rand value, and for each Character on higher index the damage taken is only randomized from higher up the scale.

public int GetDamage(int low, int high)
{
    Random r = new Random();
    int rand = r.Next(low, high);
    return rand;
}

This is the randomizer I use. Then I update the health left like this:

int Damage = GetDamage(3, 10);
Characters[Target].Health = Characters[Target].Health - Damage;

In this example dmg is divided like this:

Number 1: 3-4
Number 2: 4-6
Number 3: 5-7
Number 4: 7-8
Number 5: 8-9

解决方案

You have to reuse the same random instance otherwise you won't get really random values since it is created with the current time as seed. If you call GetDamage very fast(e.g. in a loop) you will get always the same values.

So either use a field/property in the class of GetDamage or pass the random instance to the method.

private Random _rnd = new Random();
public int GetDamage(int low, int high)
{
    int rand = _rnd.Next(low, high);
    return rand;
}

MSDN

The random number generation starts from a seed value. If the same seed is used repeatedly, the same series of numbers is generated. One way to produce different sequences is to make the seed value time-dependent, thereby producing a different series with each new instance of Random. By default, the parameterless constructor of the Random class uses the system clock to generate its seed value, while its parameterized constructor can take an Int32 value based on the number of ticks in the current time. However, because the clock has finite resolution, using the parameterless constructor to create different Random objects in close succession creates random number generators that produce identical sequences of random numbers. This problem can be avoided by creating a single Random object rather than multiple ones.