Random number generation between two floats

I am trying to create a random number between two floats (max increases by half)

this is what i have so far but it doesn't work

//range NSString *minString = [dict objectForKey:@"min"]; float minRange = [minString floatValue]; NSString *maxString = [dict objectForKey:@"max"]; float maxRange = [maxString floatValue]; NSLog(@"the ORIGINAL range is %f - %f", minRange, maxRange); maxRange = maxRange + (maxRange/2); //If you want to get a random integer in the range x to y, you can do that by int randomNumber = (arc4random() % y) + x; float randomNumber = (arc4random() % maxRange) + minRange; //ERROR: "Invalid operands to binary expression ('float' and 'float') NSLog(@"the range is %f - %f", minRange, maxRange); NSLog(@"the random number is %f", randomNumber); 
+6
source share
2 answers

Include:

 #define ARC4RANDOM_MAX 0x100000000 

And then try the following:

 double val = ((double)arc4random() / ARC4RANDOM_MAX) * (maxRange - minRange) + minRange; 
+18
source

Of course this is:

 float randomNumber = ((float)arc4random() / ARC4RANDOM_MAX * (maxRange - minRange)) + minRange; 
+2
source

Source: https://habr.com/ru/post/891690/


All Articles