• PL
  • EN

Jak wylosować liczbę?

W różnych programach komputerowych lub grach zdarza się, że oczekujemy pojawienia się losowości. Dla przykładu w programach edukacyjnych losowe pojawianie się pytań, w grach pojawianie się obiektów lub przeciwników na mapie w różnych miejscach itp.
Warto wiedzieć, że losowość w przypadku maszyn jest czymś umowmym, ponieważ generowanie losowości odbywa się według określonego schematu. Dlatego liczby losowe w programowaniu nazywa się także liczbami pseudolosowymi.

Aby skorzystać z funkcji losującej, niezbędne jest dołączenie specjalnej biblioteki:

#include <cstdlib>

Polecenie pozwalające na wylosowanie liczby i zapisanie jej do zmiennej wygląda następująco:

int los = rand();

To polecenie generuje liczbę według stałego i niezmiennego schematu, dlatego zawsze będzie to taka sama liczba. Aby ten problem rozwiązać dodajemy element zmienny w postaci czasu. Dlatego oprócz powyższej biblioteki wymagana jest jeszcze biblioteka CTIME:

#include <ctime>

Dodaje to do algorytmu generującego liczbę, element zmienny. Oba w połączeniu sprawiają wrażenie, że liczba jest losowa. Chociaż w każdej sekundzie wylosowana liczba będzie inna, znając czas oraz algorytm generowania liczby pseudolosowej, jesteśmy w stanie obliczyć jaka to będzie liczba. Dlatego zanim użyjemy polecenia rand(), należy dodać srand().

WARTO WIEDZIEĆ!

Funkcja srand(time(NULL)); używa zegara komputera w celu dodawania dodatkowego czynnika w procesie generowania liczby w każdej kolejnej sekundzie. Dzięki temu co sekundę, wygenerowana liczba jest inna. Jeśli kilka liczb zostanie wygenerowanych w tej samej sekundzie, liczby losowe będą takie same. Właśnie dlatego wylosowane w ten sposób liczby nazywa się PSEUDOLOSOWYMI. Z prawdziwym losem nie ma to wiele wspólnego.

Losowanie liczb z określonego zakresu

Jeśli chcesz podać ściśle określony zakres liczbowy, skorzystaj z poniższego schematu:

int los = x + rand() % (y - x + 1); Program wylosuje liczbę całkowitą z zakresu od x : y.
int los = rand() % (y + 1); Program wylosuje liczbę całkowitą z zakresu od 0 : y.

double los = x + (double) rand() / RAND_MAX* (y-x); Program wylosuje liczbę z zakresu od x : y.


Przykład praktyczny losowania liczby całkowitej z zakresu od 1 : 100:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main () {

    srand(time(NULL));
    int los = 1 + rand() % 101;
    cout << "Wylosowano: " << los;

}

How to draw a number?

In various computer programs or games it happens that we expect randomness to appear. For example, in educational programs random appearance of questions, in games the appearance of objects or opponents on the map in different places, etc.
It is worth knowing that randomness in the case of machines is something conventional, because the generation of randomness takes place according to a specific. That is why random numbers in programming are also called pseudorandom numbers.

To use the draw function, it is necessary to include a special library:

#include <cstdlib>

The command to draw a number and store it in a variable is as follows:

int fate = rand();

This command generates a number according to a fixed and unchanging scheme, so it will always be the same number. To solve this problem we add a variable element in the form of time. Therefore, in addition to the above library, you need CTIME library:

#include <ctime>

This adds a variable element to the algorithm that generates the number. The two in combination give the impression that the number is random. However, each second the number drawn will be different, knowing the time and the algorithm for generating the pseudorandom number, we are able to calculate what number it will be. So before we use the command rand(), we have to add srand().

GOOD TO KNOW!

Function srand(time(NULL)); uses the computer's clock to add an extra factor in the process generating a number in each successive second. In this way, every second, the number generated is different. If multiple numbers are generated in the same second, the random numbers will be the same. This is why the numbers drawn in this way are called pseudorandom. It has little to do with true randomness.

Drawing numbers from a specified range.

If you want to specify a strict numerical range, use the following diagram:

int fate = x + rand() % (y - x + 1); The program will draw an integer from the range x : y.
int fate = rand() % (y + 1); The program will draw an integer from the range 0 : y.

double fate = x + (double) rand() / RAND_MAX* (y-x); The program will draw a number from the range x : y.


Practical example of drawing an integer from the range 1 : 100:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main () {

    srand(time(NULL));
    int fate = 1 + rand() % 101;
    cout << "The number drawn is: " << fate;

}