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

Kursinnehåll

C++ Intermediate | Mobile-Friendly

C++ Intermediate | Mobile-Friendly

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

book
Random

In the future steps, we will work with multiple arrays simultaneously. It can be a bit painful to fill them manually. Let’s write the program, which will fill the array with random values. To generate a random number, you can use the function rand():

12
int a = rand(); cout << a;
copy

Too large, isn’t it? To set restrictions on the range in which you want to get a number, use the symbol %. For instance, the following code will print a random number less than 10:

python

You can also set the start of the required range. Just add +. The following program will write the random number in the range of 10 to 20:

python

Now we know how to get the random value. Let’s fill the array with random numbers from 1 to 20!

1234
for (int i = 0; i < 5; i++){ &nbsp;&nbsp;&nbsp;&nbsp;numbers[i] = rand() % 20 + 1; &nbsp;&nbsp;&nbsp;&nbsp;cout << numbers[i] << " "; }
copy

Random is often used in game development. Let’s play!

question-icon

Let's write the dice! You should make a program that outputs random values from 1 to 6.

int dice =;
cout <<
;
3

Click or drag`n`drop items and fill in the blanks

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 4

Fråga AI

expand
ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

course content

Kursinnehåll

C++ Intermediate | Mobile-Friendly

C++ Intermediate | Mobile-Friendly

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

book
Random

In the future steps, we will work with multiple arrays simultaneously. It can be a bit painful to fill them manually. Let’s write the program, which will fill the array with random values. To generate a random number, you can use the function rand():

12
int a = rand(); cout << a;
copy

Too large, isn’t it? To set restrictions on the range in which you want to get a number, use the symbol %. For instance, the following code will print a random number less than 10:

python

You can also set the start of the required range. Just add +. The following program will write the random number in the range of 10 to 20:

python

Now we know how to get the random value. Let’s fill the array with random numbers from 1 to 20!

1234
for (int i = 0; i < 5; i++){ &nbsp;&nbsp;&nbsp;&nbsp;numbers[i] = rand() % 20 + 1; &nbsp;&nbsp;&nbsp;&nbsp;cout << numbers[i] << " "; }
copy

Random is often used in game development. Let’s play!

question-icon

Let's write the dice! You should make a program that outputs random values from 1 to 6.

int dice =;
cout <<
;
3

Click or drag`n`drop items and fill in the blanks

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 4
Vi beklagar att något gick fel. Vad hände?
some-alt