What is rand?and what is the use of rand function. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What is rand?and what is the use of rand function.

please give the live example.

6th Nov 2016, 5:36 PM
Shivam Jamaiwar
Shivam Jamaiwar - avatar
3 Answers
+ 3
rand () is a built in c++ function that returns a random number when it is called. This function is found in the cstdlib hence you need to include the cstdlib everytime you plan to use it syntax: num = rand (); NB:Dont forget to #include <cstdlib> DevC++ also accepts <stdlib.h> This will return a random number from your computer. num = rand % 6; This returns a number between 1 and 6 (range is specified by the ' % 6 ' ). However this has a problem of returning the same number each time the code is run, and that is why rand () is usually used with the srand () function, also found in the cstdlib
6th Nov 2016, 6:40 PM
Tawanda Mthembo
Tawanda Mthembo - avatar
+ 1
rand() returns a random value. This is useful in any game and in simulating probabilities. For example in a rock, paper, scissor game, you would use 1+rand()%3 to obtain a random number from 1 to 3 and use that as the opponents choice.
26th Dec 2016, 4:46 PM
bill rex
bill rex - avatar
0
PA's critical hit must be a random odds
24th Nov 2016, 5:20 AM
yifanwz
yifanwz - avatar