【发布时间】:2021-09-03 11:55:50
【问题描述】:
我正在尝试从 python 转换为 C#
蟒蛇:
rand = random.randint(0, 2 ** 50)
这就是我在 C# 中所做的
var value = Math.Pow(2, 50);
var result = random.Next(0, Convert.ToInt32(value));
我收到了这个错误
Unhandled exception. System.OverflowException: Value was either too large or too small for an Int32.
at System.Convert.ToInt32(Double value)
我想生成从 0 到值的范围
【问题讨论】:
-
您是否需要将值设为
int并且的范围也介于 0 和 2^50 之间?可以是long吗? -
An
int(Int32) 只能保持接近 2^31 的值,所以:是的,如果您要求 2 中的值,它会溢出很多时间^50 范围;long可以处理 2^63 - 也许更有用? -
而不是
Math.Pow(2, 50);使用1L << 50。在 python 中使用1 << 50 -
@harold 抱歉,我编辑了我的问题,它应该很长
标签: c#