-5

Note: I cannot use any of the built in functions to solve this.

This is the question:

Given a function that returns a random integer number between 1 and 5, create a function that creates a random integer between 1 and 7.

H H H
  • 489
  • 1
  • 7
  • 20
  • yeah its a question i got in interview! – H H H Nov 21 '13 at 07:49
  • what did you try and where did you get stuck? (sounds like a homework thread if you cant use builtin stuff?) – usethedeathstar Nov 21 '13 at 07:49
  • pfft. this is homework and you clearly haven't made the slightest effort. – SpliFF Nov 21 '13 at 07:49
  • alright... im removing it.. – H H H Nov 21 '13 at 07:49
  • 1
    @SpliFF This is a somewhat famous google interview question. That already has an answer on this site – daniel gratzer Nov 21 '13 at 07:50
  • 1
    Perfect solution is here http://xkcd.com/221/ – thefourtheye Nov 21 '13 at 07:50
  • I think the question itself is a joke / trick question. A function that returns a random integer number between 1 and 5 ALREADY meets the second criteria because every number in the range 1 - 5 is also between 1 and 7 – SpliFF Nov 21 '13 at 07:53
  • This is an interesting question (although it should show an attempt - I can think of a number of invalid approaches), *but the title is absolutely horrid*. – user2864740 Nov 21 '13 at 07:53
  • @Spliff it most likely have to have a quite even distribution between the numbers when it has been run a lot of times, else it really makes no sense. – spydon Nov 21 '13 at 07:55
  • http://stackoverflow.com/questions/137783/expand-a-random-range-from-15-to-17 eh...answered a long time ago. – Zegar Nov 21 '13 at 08:02

3 Answers3

1

Apparently the following doesn't work. See the linked answer and the other responses as well.


Well, here is an approach I am thinking of. It could be wrong. I am going with the assumption that the result should be randomly distributed within the larger range.

Generate 7 random numbers using the given random functions (that generates numbers [1,5]) and calculate their sum.

This will result in a value between 7 (1 * 7) and 35 (5 * 7).

Because this value is evenly divisible by the target range and randomly distributed, then it seems like it would be valid to collapse the intermediate range [7,35] back to [1,7] without losing uniformity.

Or maybe that's not quite it - but it seems like exploiting a common multiple between the numbers is key.

Community
  • 1
  • 1
user2864740
  • 60,010
  • 15
  • 145
  • 220
1

As it has been answered in Adam Rosenfield's solution a while ago you may try to use an algoritm similar to this one below:

int i;
do
{
  i = 5 * (rand5() - 1) + rand5();  // i is now uniformly random between 1 and 25
} while(i > 21);
// i is now uniformly random between 1 and 21
return i % 7 + 1;  // result is now uniformly random between 1 and 7

EDIT: Translation to python:

def rand7():
i = 0;

while True:
    i = 5 * (rand5() -1) + rand5()
    if i > 21:
        break
return i % 7 + 1
Community
  • 1
  • 1
Zegar
  • 1,005
  • 10
  • 18
  • But all right I've added a python version. Please check it as I'm not experienced in this language. – Zegar Nov 21 '13 at 08:42
-1
import random
def winningFunc():
   return originalFuncReturning1to5() + random.randint(0,2)
SpliFF
  • 38,186
  • 16
  • 91
  • 120