# Rand5() to Rand7() [closed]

You are provided with a function Rand5(). This function returns perfectly random (equal distribution) integers between 1 and 5.

Provide the function Rand7(), which uses Rand5() to produce perfectly random integers between 1 and 7.

• Duplicate question – Mateen Ulhaq Jun 17 '11 at 5:23
• Obligatory xkcd: xkcd.com/221 – Steven Rumbalski Sep 28 '11 at 14:39
• 1 and 5 inclusive? i.e. from the set {1,2,3,4,5} ? – Aaron McDaid Dec 8 '11 at 22:54
• What criteria determines a single winner? – kojiro Jan 26 '13 at 20:51
• That moment when you realize this is actually an old question. – nyuszika7h Dec 20 '14 at 12:33

function Rand7(){return Math.abs(Rand5()-3)+Rand5()}

function Rand7(){return abs(Rand5()-3)+Rand5();}

• The resulting distribution is not uniform. For instance the chance of pulling a seven is P(7) = 0.4 * 0.2 = 0.08 != 1/7. – dmckee --- ex-moderator kitten Apr 29 '11 at 20:20