Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Random Not Random | Data Types and Arrays
C++ Intermediate | Mobile-Friendly
course content

Contenuti del Corso

C++ Intermediate | Mobile-Friendly

C++ Intermediate | Mobile-Friendly

1. Data Types and Arrays
2. References & Pointers
3. Dynamic Memory
4. Functions

book
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:

12
srand(42); cout << rand();
copy

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.

python

Remember, you should use the function srand() only once to set the seed value.

question mark

Which lines of code do you need to make truly random values?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 5

Chieda ad AI

expand
ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

course content

Contenuti del Corso

C++ Intermediate | Mobile-Friendly

C++ Intermediate | Mobile-Friendly

1. Data Types and Arrays
2. References & Pointers
3. Dynamic Memory
4. Functions

book
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:

12
srand(42); cout << rand();
copy

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.

python

Remember, you should use the function srand() only once to set the seed value.

question mark

Which lines of code do you need to make truly random values?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 5
Siamo spiacenti che qualcosa sia andato storto. Cosa è successo?
some-alt