作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在使用这段代码,其中“长度”值为“50”。
newX = (arc4random()%(lenght+1)) - (lenght/2);
newY = (arc4random()%(lenght+1)) - (lenght/2);
NSLog(@"Creature Move X:%f, Y:%f", newX, newY);
但是在调试器中我得到了类似的东西:
2012-01-02 21:10:50.794 Kipos[28833:207] Creature Move X:4294967296.000000, Y:4294967296.000000
2012-01-02 21:10:50.896 Kipos[28833:207] Creature Move X:4294967296.000000, Y:12.000000
发生了什么事?
newX
和 newY
是 float :
float newX;
float newY;
最佳答案
arc4random
返回一个 unsigned int(并且大概 length
也是无符号的)。将您的代码更改为例如
newX = (float)((int)(arc4random() % (length + 1))) - (length / 2));
为了避免减法时溢出。
请注意,我还为结果添加了一个显式浮点转换,这不是绝对必要的,但它使代码更加不言自明。
关于objective-c - 尴尬的 arc4random 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8704951/
我想为进入 C# web api 应用程序的一些请求编写一个简单的负载均衡器。 (我只使用 C# 的东西作为创建 Web 服务器的便捷方式)。 解决这个问题的最佳方法是什么? (我还没有真正在 F#
作为一名数据库开发人员,当我尝试将仅数据转储到 PostgreSQL(10.1) 数据库“tlesson”时,我遇到了这个通知。 通知=> pg_dump: NOTICE: there are c
我是一名优秀的程序员,十分优秀!