-1

Given a function F that generates random numbers between 1 and 5, how would I write a function G, using F, to generate random numbers between 1 and 7?

The probability of each number generated by G should be the same.

So far, I have tried the equation G=F + (5*(F/7)), although I'm not sure that all of the probabilities are the same.

APerson
  • 8,140
  • 8
  • 35
  • 49
Lav
  • 1,283
  • 5
  • 21
  • 48
  • wanted to know if its the correct solution ... guess its not ... above is not equal probability ... found this ... http://stackoverflow.com/questions/137783/expand-a-random-range-from-15-to-17?rq=1 – Lav Feb 16 '13 at 03:26

1 Answers1

0

Pseudocode:

function g()
    repeat
        // result will be equidistributed in 0..24
        result = (f()-1)*5 + (f()-1);
        // wait till we have something below 21, on average this happens 21 out of 25 times in the first pass
        if (result < 21): return 1 + (result % 7)
Udo Klein
  • 6,784
  • 1
  • 36
  • 61