What is the real use of rand() in programming? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

What is the real use of rand() in programming?

Where does random numbers can be used in any programming?

16th Jan 2017, 5:14 AM
Subhodip Mitra
Subhodip Mitra - avatar
16 Answers
+ 11
Random numbers are used anywhere you want things to be unpredictable/natural. Eg. 1. For storing password in database advanced random generator is used. 2. For particle animation in games random functions are used to randomise the movement of particles. 3. In games, random functions are used to randomise the behaviour of AI. The list goes on. *** Remember, real world applications do not use the simple rand() function, but more complex versions of it.***
16th Jan 2017, 6:29 AM
Sidharth Ravi
Sidharth Ravi - avatar
+ 10
They are used in game programming (to make an AI unpredictable for example).
16th Jan 2017, 5:42 AM
Karl T.
Karl T. - avatar
+ 5
Random numbers. Use it with a timestamp for better results, and don't forget to include <ctime>.
19th Jan 2017, 1:13 AM
Mark Foxx
Mark Foxx - avatar
+ 3
generate random data for tests select a value from a range (attack damage) to choose a random name for a monster random drops in rpgs random generated maps to make unpredictable things
16th Jan 2017, 1:26 PM
DFX
DFX - avatar
+ 3
Random distributions are used in computer graphics for rendering, cryptography for key generation codes, and graph search of complex spaces.
17th Jan 2017, 12:55 AM
Marc
+ 3
#include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main () { srand(time(0)); if(rand() % 13)cout<<"Today Sunday"; else cout<<"You must go to work/school"; return 0; } /*if output is "You must go to work/school", it may be wrong answer (program sometimes does mistakes) ,so try again*/
12th Apr 2017, 7:21 PM
Mark Anohin
Mark Anohin - avatar
+ 2
thanks for your answer
16th Jan 2017, 6:56 AM
Subhodip Mitra
Subhodip Mitra - avatar
+ 2
rand() is in-built function to generate random number. You can use this with some code to generate password (not good though), for card game, shuffling etc
16th Jan 2017, 9:14 PM
RanjOyHunTer
RanjOyHunTer - avatar
+ 1
to get random values
16th Jan 2017, 6:43 PM
Vikas hb raj
Vikas hb raj - avatar
+ 1
Generating password randomly.. Orr creating random id for database. Or something where thing should be unpredictable
17th Jan 2017, 8:43 PM
Akash Tripathi
Akash Tripathi - avatar
+ 1
Another use I didn't see listed here that I like to use it for is testing a function/method. You might make a function where you pass in a value that has to be within a certain range. You can test this by using your language's random number generator to batter the function with numbers that fit your parameters, and you can confirm that it returns the correct info depending on its input.
17th Jan 2017, 10:09 PM
Brendan Lyon
Brendan Lyon - avatar
+ 1
c++
17th Jan 2017, 10:40 PM
Lacey Harris
Lacey Harris - avatar
+ 1
rand() function is useful to generate random numbers. so if we are working in a project rand() is used to generate id numbers
18th Jan 2017, 7:10 AM
Vishnu Sivan
Vishnu Sivan - avatar
+ 1
you will require random generator a lot in competitive coding or competition to generate the test cases for your program..and other use are mentioned above
18th Jan 2017, 8:43 AM
Ayush Mishra
Ayush Mishra - avatar
+ 1
Key usage of Rand() is to generate passwords, OTPs and in gaming
18th Jan 2017, 8:47 AM
Manohar B
Manohar B - avatar
+ 1
rand function is used to perform random numbers, lets say you have a set of students you want them to have like a random drug test or something but you want it to be automated, you'll use the random to pick a certain id number that you will subject to the drug test. more over than not, you'll find rand useful in games mostly, but also in a code which is based on aiming a luck or chance
18th Jan 2017, 10:26 AM
Mark L. M.
Mark L. M. - avatar