I'm trying to create a random float between 0.15 and 0.3 in Objective-C. The following code always returns 1:
我正在尝试在Objective-C中创建0.15到0.3之间的随机浮点数。以下代码始终返回1:
int randn = (random() % 15)+15;
float pscale = (float)randn / 100;
What am I doing wrong?
我究竟做错了什么?
8 个解决方案
#1
18
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.
有几种方法可以做同样的事情。
#2
63
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;
}
#3
4
- use arc4random() or seed your random values
- 使用arc4random()或种子随机值
-
try
尝试
float pscale = ((float)randn) / 100.0f;
#4
3
Your code works for me, it produces a random number between 0.15 and 0.3 (provided I seed with srandom()
). Have you called srandom()
before the first call to random()
? You will need to provide srandom()
with some entropic value (a lot of people just use srandom(time(NULL))
).
你的代码适用于我,它产生一个0.15到0.3之间的随机数(假设我用srandom()种子)。你是否在第一次调用random()之前调用了srandom()?你需要为srandom()提供一些熵值(很多人只使用srandom(time(NULL)))。
For more serious random number generation, have a look into arc4random
, which is used for cryptographic purposes. This random number function also returns an integer type, so you will still need to cast the result to a floating point type.
对于更严重的随机数生成,请查看arc4random,它用于加密目的。此随机数函数也返回整数类型,因此您仍需要将结果转换为浮点类型。
#5
2
Easiest.
最容易的。
+ (float)randomNumberBetween:(float)min maxNumber:(float)max
{
return min + arc4random_uniform(max - min + 1);
}
#6
0
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)*. It allows to generate true random integer from zero to *upper_bound*.
在MAC_10_7,IPHONE_4_3及更高版本上,您可以使用arc4random_uniform(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);
#7
0
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。
#8
-5
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”函数将它解析为floatvalue ...我找不到更好的方法,这对我的意图有效,希望它有帮助:)
int integervalue = arc4random() % 2;
int decimalvalue = arc4random() % 9;
NSString *floatString = [NSString stringWithFormat:@"%d.%d",integervalue,decimalvalue];
float randomFloat = [floatString floatValue];
#1
18
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.
有几种方法可以做同样的事情。
#2
63
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;
}
#3
4
- use arc4random() or seed your random values
- 使用arc4random()或种子随机值
-
try
尝试
float pscale = ((float)randn) / 100.0f;
#4
3
Your code works for me, it produces a random number between 0.15 and 0.3 (provided I seed with srandom()
). Have you called srandom()
before the first call to random()
? You will need to provide srandom()
with some entropic value (a lot of people just use srandom(time(NULL))
).
你的代码适用于我,它产生一个0.15到0.3之间的随机数(假设我用srandom()种子)。你是否在第一次调用random()之前调用了srandom()?你需要为srandom()提供一些熵值(很多人只使用srandom(time(NULL)))。
For more serious random number generation, have a look into arc4random
, which is used for cryptographic purposes. This random number function also returns an integer type, so you will still need to cast the result to a floating point type.
对于更严重的随机数生成,请查看arc4random,它用于加密目的。此随机数函数也返回整数类型,因此您仍需要将结果转换为浮点类型。
#5
2
Easiest.
最容易的。
+ (float)randomNumberBetween:(float)min maxNumber:(float)max
{
return min + arc4random_uniform(max - min + 1);
}
#6
0
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)*. It allows to generate true random integer from zero to *upper_bound*.
在MAC_10_7,IPHONE_4_3及更高版本上,您可以使用arc4random_uniform(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);
#7
0
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。
#8
-5
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”函数将它解析为floatvalue ...我找不到更好的方法,这对我的意图有效,希望它有帮助:)
int integervalue = arc4random() % 2;
int decimalvalue = arc4random() % 9;
NSString *floatString = [NSString stringWithFormat:@"%d.%d",integervalue,decimalvalue];
float randomFloat = [floatString floatValue];