Domanda di colloquio di 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).

Risposte di colloquio

Anonimo

20 feb 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

Anonimo

5 ott 2017

Just use the modulo operator.

Anonimo

1 nov 2017

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