Rand() data type | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Rand() data type

Does the rand() function only work with int data type? Or can i use a floating point values? Thanks!

21st Feb 2020, 7:03 PM
CLEO
CLEO - avatar
2 Answers
+ 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 )
21st Feb 2020, 7:17 PM
CLEO
CLEO - avatar
+ 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] */ }
21st Feb 2020, 7:19 PM
Trần Tân An Nguyễn
Trần Tân An Nguyễn - avatar