+ 2
What is rand?and what is the use of rand function.
please give the live example.
4 Réponses
+ 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
+ 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.
0
PA's critical hit must be a random odds