除以随机.次数总是会导致0?[英] Dividing by Random.next always results in 0?

本文是小编为大家收集整理的关于除以随机.次数总是会导致0?的处理方法,想解了除以随机.次数总是会导致0?的问题怎么解决?除以随机.次数总是会导致0?问题的解决办法?除以随机.次数总是会导致0?问题的解决方案?那么可以参考本文帮助大家快速定位并解决问题,译文如有不准确的地方,大家可以切到English参考源文内容。

问题描述

这个真的让我感到困惑.我正在编写具有随机变化的损坏算法.当我计算变化时,这就是它的外观.

Random random = new Random();
Double variation = random.Next(85, 115) / 100;
Double damage = restOfAlgorithm * variation;

当我这样做时,变化总是输出0.但是,如果我喜欢下面,它将输出预期的结果.

Random random = new Random();
Double variation = random.Next(85, 115);
Double damage = restOfAlgorithm * (variation / 100);

为什么会发生这种情况?

推荐答案

除以双重:

Double variation = random.Next(85, 115) / 100.0;

Double variation = random.Next(85, 115) / (double)100;

否则您将进行整数算术(因为Random.Next返回整数,而100也是整数).

我认为最好的做法是知道您正在使用哪种类型并将所有内容投入到所需类型上.当然,这是不需要的,因为编译器将隐式转换值.但是,有了明确的施放您的意图,您的意图就可以看到以后查看代码的人.

Double variation = (double)random.Next(85, 115) / 100d;

其他推荐答案

因为random.Next会给您一个整数,而100也是整数.因此,将它们划分为0或1,具体取决于您的随机数,如果您的随机数(如果低于100).

其他推荐答案

方法random.Next(lower_bound, upper_bound)返回整数值(int type).在第一种情况下,您有random.Next(85, 115) / 100,这是int变量的一个划分.在C#中,它意味着整数部门(当数字和INT时).这就是为什么有时会返回0的原因. 在第二种情况下,您使用variation type double.这就是为什么c#将结果转换为两倍,然后一切正常.

本文地址:https://www.itbaoku.cn/post/2047432.html

问题描述

This one is really puzzling me. I am writing a damage algorithm with some random variation. When I calculate the variation this is what it looks like.

Random random = new Random();
Double variation = random.Next(85, 115) / 100;
Double damage = restOfAlgorithm * variation;

When I do that, variation always outputs 0. However, if I do like below, it will output the expected result.

Random random = new Random();
Double variation = random.Next(85, 115);
Double damage = restOfAlgorithm * (variation / 100);

Why does this happen?

推荐答案

Divide by a double:

Double variation = random.Next(85, 115) / 100.0;

or

Double variation = random.Next(85, 115) / (double)100;

Otherwise you'll be doing integer arithmetic (since Random.Next returns an integer and 100 is also integer).

I consider it best practice to know what types you are working with and cast everything to the type desired. Certainly this is more than is necessary, as the compiler will implicitly convert values. But with explicit casts your intentions are then visible to someone looking at the code later.

Double variation = (double)random.Next(85, 115) / 100d;

其他推荐答案

Because random.Next will give you an integer and 100 is also an integer. So dividing them will either result in 0 or 1 depending if your random number if below or above 100. You need to divide by 100.0 in order to get a double division.

其他推荐答案

The method random.Next(lower_bound, upper_bound) returns an integer value (int type). In the first case you have random.Next(85, 115) / 100 and this is a division of int variables. In C# it implies integer division (when both numbers and ints). That is why it sometimes returns 0.
In the second case you use variation which is of type double. That is why C# converts the result to double and then everything is OK.

查看更多