Question d’entretien chez WD

Given a function that generates a random integer in the range of [0, MAX), write a function that gets an integer x as input and returns a random integer in [0, x).

Réponses aux questions d'entretien

Utilisateur anonyme

20 févr. 2020

This code should practically work: int rand(int x) { int num = MAX - (MAX % x); out = given_rand(); while (out >= num) out = given_rand(); return (out % x); }

1

Utilisateur anonyme

5 oct. 2017

Just use the modulo operator.

Utilisateur anonyme

1 nov. 2017

Using modulo is wrong, because you must have an equal probability to each element.