Generating random numbers
Sometimes you need to add a little bit of chaos to the code. Well, literally:
result = rand(); — and voila, you got a random number.mber.
What you do with it is up to you: check the algorithm, randomly place enemies in the game, simulate the queue in the bank ... there are a lot of options.
The rand() function is your personal randomness genss generator. Well, almost. In fact, she is not a magician, but works according to a completely predictable algorithm, it's just that this algorithm is well disguised as chaos.
That is, there is no absolute randomness — it all depends on the initial conditions (or the seed, if you want to use programming).
The main thing to understand is: you choose which numbers you need and in what form. If you want whole ones, it's not a question. A floating point? May.
In a Gaussian distribution? It's going to take a bit of tinkering, but it's real. Some implementations allow you to set the range directly, others do not, you need to scale manually.
And that's where the nuances begin. Reliability, speed, repeatability — it all depends on which generator you use and how you configure it.
Do you need to generate a million random numbers quickly? Catch the performance limitations. I would like the results to always be repeated — copy the data.
In practice, everything is simple: you know what you need — use rand() wisely. If you don't know, you get into trouble. The main thing is not to treat this function like a magic cube.
She can do a lot, but she also requires attention.
So if you need a little randomness for tests, games, or simulations, rand() will help.
Just don't forget: a random number in programming is never completely random. But this, by the way, is not always a minus.
Help on built-in functions
The rules of programming scripts
Programm options
Color constants