Pergunta de entrevista da empresa 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).

Respostas da entrevista

Sigiloso

20 de fev. de 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

Sigiloso

5 de out. de 2017

Just use the modulo operator.

Sigiloso

1 de nov. de 2017

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