Зміст курсу
C++ Intermediate | Mobile-Friendly
C++ Intermediate | Mobile-Friendly
Random Not Random
The random we made in the previous chapter is not random values. You can run your program twice and see that nothing changes.
For this purpose, you can use the function srand()
, which sets the seed value, the number, which will be the breakpoint for the program to get a random value. For instance:
srand(42); cout << rand();
Wait, we used a random value in the program to generate a random value? It’s not the best idea for the seed parameter. To avoid such problems, you can use the time displayed on your computer as the seed. The procedure is not so complicated. You need only use time(0)
in srand()
function and add ctime
to your includes.
Remember, you should use the function srand()
only once to set the seed value.
Дякуємо за ваш відгук!