Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate a random float number in Objective-C with min and max? [duplicate]

Tags:

objective-c

Possible Duplicates:
Generating Random Numbers in Objective-C
What's the most optimal way to get a random floatingpoint number between floatA and floatB?

Hi,

I want to generate a random float number like

float scale = randFloat(0.5f, 2.0f);

How do I do this in objective-c?

like image 663
UpCat Avatar asked Dec 12 '25 17:12

UpCat


1 Answers

Here is how you generate them :

// Get random value between 0 and 99
int x = arc4random() % 100;

// Get random number between 500 and 1000
int y =  (arc4random() % 501) + 500);

You can easily extend this for any range you need.

Hope that helps.

like image 194
Shaunak Avatar answered Dec 14 '25 12:12

Shaunak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!