作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我试图在 Objective-C 中创建一个介于 0.15 和 0.3 之间的随机 float 。以下代码始终返回 1:
int randn = (random() % 15)+15;
float pscale = (float)randn / 100;
我做错了什么?
最佳答案
这是一个函数
- (float)randomFloatBetween:(float)smallNumber and:(float)bigNumber {
float diff = bigNumber - smallNumber;
return (((float) (arc4random() % ((unsigned)RAND_MAX + 1)) / RAND_MAX) * diff) + smallNumber;
}
关于objective-c - 如何在 Objective-C 中创建随机 float ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3410070/
我是一名优秀的程序员,十分优秀!