简体   繁体   中英

ios how to generate a int random number of float range

I'm getting a random number float value:

float myFloat=120.12123123123;
int tmpInt = floorf(myFloat);
int ran=rand()%tmpInt;

But I'm trying to do everything one line:

float myFloat=120.12123123123;
int ran=rand()%([[floorf(myFloat)] intValue]);

but on this line I'm getting the following error:

"int ran=rand()%([[floorf(myFloat)] intValue]);" error : "Expeted identifier"

How can I fix this?

I'll really appreciate your help.

float is a primitive type, so you can just cast it. For example,

int ran = rand() % (int)floorf(myFloat);

The compiler might not even require the (int) cast...

You might want to look at this relevant SO question: What is a Type Cast?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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