简体   繁体   English

如何在 Objective-C 中创建随机浮点数?

[英]How to create a random float in Objective-C?

I'm trying to create a random float between 0.15 and 0.3 in Objective-C.我正在尝试在 Objective-C 中创建一个介于 0.15 和 0.3 之间的随机浮点数。 The following code always returns 1:以下代码始终返回 1:

int randn = (random() % 15)+15;
float pscale = (float)randn / 100;

What am I doing wrong?我究竟做错了什么?

Here is a function这是一个函数

- (float)randomFloatBetween:(float)smallNumber and:(float)bigNumber {
    float diff = bigNumber - smallNumber;
    return (((float) (arc4random() % ((unsigned)RAND_MAX + 1)) / RAND_MAX) * diff) + smallNumber;
}

Try this:尝试这个:

 (float)rand() / RAND_MAX

Or to get one between 0 and 5:或者得到 0 到 5 之间的一个:

 float randomNum = ((float)rand() / RAND_MAX) * 5;

Several ways to do the same thing.有几种方法可以做同样的事情。

  1. use arc4random() or seed your random values使用 arc4random() 或种子你的随机值
  2. try尝试

    float pscale = ((float)randn) / 100.0f;

Your code works for me, it produces a random number between 0.15 and 0.3 (provided I seed with srandom() ).您的代码对我srandom() ,它会产生一个介于 0.15 和 0.3 之间的随机数(前提是我用srandom()种子)。 Have you called srandom() before the first call to random() ?您在第一次调用random()之前调用过srandom() random()吗? You will need to provide srandom() with some entropic value (a lot of people just use srandom(time(NULL)) ).您需要为srandom()提供一些熵值(很多人只使用srandom(time(NULL)) )。

For more serious random number generation, have a look into arc4random , which is used for cryptographic purposes.对于更严重的随机数生成,请查看用于加密目的的arc4random This random number function also returns an integer type, so you will still need to cast the result to a floating point type.此随机数函数也返回整数类型,因此您仍需要将结果转换为浮点类型。

Easiest.最简单。

+ (float)randomNumberBetween:(float)min maxNumber:(float)max
{
    return min + arc4random_uniform(max - min + 1);
}

Using srandom() and rand() is unsafe when you need true randomizing with some float salt.当您需要使用一些浮盐进行真正的随机化时,使用 srandom() 和 rand() 是不安全的。

On MAC_10_7, IPHONE_4_3 and higher you can use arc4random_uniform(upper_bound)*.在 MAC_10_7、IPHONE_4_3 和更高版本上,您可以使用 arc4random_uniform(upper_bound)*。 It allows to generate true random integer from zero to *upper_bound*.它允许生成从零到 *upper_bound* 的真正随机整数。

So you can try the following所以你可以试试下面的

u_int32_t upper_bound = <some big enough integer>;

float r = 0.3 * (0.5 + arc4random_uniform(upper_bound)*1.0/upper_bound/2);

To add to @Caladain's answer, if you want the solution to be as easy to use as rand() , you can define these:要添加到@Caladain 的答案中,如果您希望解决方案像rand()一样易于使用,您可以定义这些:

#define randf() ((CGFloat)rand() / RAND_MAX)
#define randf_scaled(scale) (((CGFloat)rand() / RAND_MAX) * scale)

Feel free to replace CGFloat with double if you don't have access to CoreGraphics.如果您无法访问 CoreGraphics,请随意将CGFloat替换为double

I ended up generating to integers one for the actual integer and then an integer for the decimal.我最终为实际整数生成了一个整数,然后为小数生成了一个整数。 Then I join them in a string then I parse it to a floatvalue with the "floatValue" function... I couldn't find a better way and this works for my intentions, hope it helps :)然后我将它们加入一个字符串,然后我用“floatValue”函数将它解析为一个浮点值......我找不到更好的方法,这对我的意图有效,希望它有帮助:)

int integervalue = arc4random() % 2;
int decimalvalue = arc4random() % 9;   
NSString *floatString = [NSString stringWithFormat:@"%d.%d",integervalue,decimalvalue];
float randomFloat = [floatString floatValue];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM