+ 6
What is the real use of rand() in programming?
Where does random numbers can be used in any programming?
16 Réponses
+ 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.***
+ 10
They are used in game programming (to make an AI unpredictable for example).
+ 5
Random numbers. Use it with a timestamp for better results, and don't forget to include <ctime>.
+ 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
+ 3
Random distributions are used in computer graphics for rendering, cryptography for key generation codes, and graph search of complex spaces.
+ 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*/
+ 2
thanks for your answer
+ 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
+ 1
to get random values
+ 1
Generating password randomly.. Orr creating random id for database. Or something where thing should be unpredictable
+ 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.
+ 1
c++
+ 1
rand() function is useful to generate random numbers. so if we are working in a project rand() is used to generate id numbers
+ 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
+ 1
Key usage of Rand() is to generate passwords, OTPs and in gaming
+ 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