Given a function which produces a random integer in the range 0 to 4, write a function which produces a random integer in the range 0 to 6.
I tried to project one of the solutions from Expand a random range from 1–5 to 1–7
int rand6()
{
int vals[4][4] = {
{ 0, 1, 2, 3},
{ 4, 5, 6, 0},
{ 1, 2, 3, 4},
{ 5, 6, -1, -1}
};
int result = -1;
while (result == -1)
{
int i = rand5();
int j = rand5();
result = vals[i-1][j-1];
}
return result;
}
- Is the probability uniform?
- How will the solution differ if the problem was from 1-4 to 1-6?
- Can I project this solution on any x-y to x-z type of questions expand random generator? And if not, is there a solution that can solve all of those qustions?
Aucun commentaire:
Enregistrer un commentaire