gpt4 book ai didi

objective-c - 如何在 Objective-C 中创建随机 float ?

转载 作者:太空狗 更新时间:2023-10-30 03:08:44 25 4
gpt4 key购买 nike

我试图在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com