FMath::Rand question

Hello, i’m having some issue getting a random number between 0 and n-1 (the max size of my array), i saw the rand is getting the max from the RAND_MAX, but this value cannot be modified because it is a constant.

sorry for this question (i’m new to c++), but i need some clues or tips on how to get a random number given the min and a max value as a parameter, i think this can be done in c++ but i don’t know how.

somebody can point to me in the right direction ?

thanks in advance for all the help.

You want to call FMath::FRand(). It will give you a value between 0 and 1. Then multiply the result by n-1 to get a value between 0 and n-1.

thank you for the information!

There is a RandInRange too :slight_smile: Could just Floor the result and cast to an Int.

i found RandRange, and i’m using in this way


just to be sure, i will receive a random integer from 0 to Max-1 right ?

Yes. As the RandRange comment mentions, it is inclusive (“Returns a random number >= Min and <= Max”). So Max-1 will properly limit returned values to the range of your array indices.


thanks for all the help !

Oh cool, I didn’t realise there was an Integer version too. Nice :slight_smile: