iOS 上的高精度随机数
我已经尝试了一段时间,但到目前为止还没有任何运气。
在 iOS 上检索两个非常精确的数字之间的随机数的最简单方法是什么?
例如,我想要一个介于 41.37783830549337 和 41.377730629131634 之间的随机数,我该如何实现呢?
提前非常感谢!
编辑:我尝试了这个:
double min = 41.37783830549337;
double max = 41.377730629131634;
double test = ((double)rand() / RAND_MAX) * (max - min) + min;
NSLog(@"Min:%lf, max:%lf, result:%lf",min,max,test);
但结果并不像我希望的那么精确,结果是这样的::
Min:41.377838, max:41.377731, result:41.377838
I have been trying this for a while but thus far haven't had any luck.
What is the easiest way to retrieve a random number between two very precise numbers on iOS?
For example, I want a random number between 41.37783830549337 and 41.377730629131634, how would I accomplish this?
Thank you so much in advance!
Edit: I tried this:
double min = 41.37783830549337;
double max = 41.377730629131634;
double test = ((double)rand() / RAND_MAX) * (max - min) + min;
NSLog(@"Min:%lf, max:%lf, result:%lf",min,max,test);
But the results weren't quite as precise as I was hoping, and ended up like this::
Min:41.377838, max:41.377731, result:41.377838
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将
rand
的输出标准化为您想要的任何范围:[注意:这是纯 C,我假设它在 Obj-C 中等效工作。]
[< em>注释 2:将
double
替换为您选择的适当数据类型。][注释 3:将
rand
替换为 random-您根据情况选择号码来源。]You can normalise the output of
rand
to any range you want:[Note: This is pure C, I'm assuming it works equivalently in Obj-C.]
[Note 2: Replace
double
with the data-type of your choice as appropriate.][Note 3: Replace
rand
with the random-number source of your choice as appropriate.]