Random numbers
Theory and problem statement
Machine translation, not yet reviewed by a native speaker.
rand() from <stdlib.h> returns a pseudo-random number from 0 to RAND_MAX:
#include <stdlib.h>
#include <time.h>
srand(time(NULL)); // "seed" the generator — once at the start of the program
int dice = rand() % 6 + 1; // from 1 to 6
They're pseudo-random because this is a deterministic formula: with the same seed (srand(42)) the sequence is always the same. This is useful for tests and reproducible simulations. Without srand the seed is 1, and the program gives the same numbers on every run.
A number in the range [lo, hi]: lo + rand() % (hi - lo + 1). This formula has a small bias (some values come up slightly more often), but it's good enough for games and learning tasks.
rand() is not suitable for cryptography and passwords: it's easy to predict. For those, system sources are used (/dev/urandom, getrandom()).
Task. Seed the generator with a number from the input (srand(seed)), generate 1000 dice rolls and print ok if all the rolls lie in the range from 1 to 6 and each of the six values came up at least once. Otherwise print bad.
Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.