+ 1
Rand() data type
Does the rand() function only work with int data type? Or can i use a floating point values? Thanks!
4 Respostas
+ 1
Ok 🤔 I'm really confused because my professor is telling us that it doesnt really work on decimal types...( he said it might work on long but because rand() returns and int, it might not )
+ 1
rand() function returns only int data type. However, you can randomize a floating point value in an interval by using this function:
float float_rand( float min, float max )
{
float scale = rand() / (float) RAND_MAX; /*Example: [0, 1.0] */
return min + scale * ( max - min ); /* [min, max] */
}