← All lectures

Lecture 11 · 20 min

Generating Random Numbers

Using rand(), RAND_MAX, and srand() to generate random integers — plus formulas for bounded ranges.

rand()RAND_MAXsrand(time(0))Bounded ranges
Slide 1 / 12

Why random numbers?

Random numbers are used everywhere in programming:

  • Games — dice rolls, card shuffles, enemy behavior
  • Simulations — modelling weather, traffic, physics
  • Security — generating passwords, encryption keys
  • Testing — creating random test data

C++ provides a simple way to generate pseudo-random numbers with the rand() function.

← / → navigateF fullscreenEsc exit⌘K search