Rand() data type | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 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 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 )
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