samedi 9 juillet 2022

Expand a random generator from 0-4 to 0-6

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;
}
  1. Is the probability uniform?
  2. How will the solution differ if the problem was from 1-4 to 1-6?
  3. 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