How to generate a uniform random variable from [1,7] if given a uniform[1,5] RV generator? I saw this as a question on glassdoor and I've seen similar questions elsewhere. Can someone explain the intuition of how to solve a problem like this? There are two scenarios


*

*The uniform RVs in question are discrete  

*The uniform RVs in question are continuous


For the continuous case, my intuition is:
say $X\sim Uniform[1,5] $, then $y = \frac{x-1}{5-1}*(7-1)+1\sim Uniform[1,7]$
Not sure if this is right. And not sure how to do the discrete case.
 A: For the continuous case, I think you have it.
For the discrete case, maybe something like this. It does the job,
but 'wastes' random numbers.
(a) Invoke the generator for 5;  
(b) invoke it again and add 6 to each
result; 
(c) concatenate to get discrete uniform sample on integers 1 through 10;
(d) reject values above 7.
In R, the function sample can be used as a discrete uniform generator:
set.seed(912)
x = sample(1:5, 1000, rep=T)              # (a)
y = sample(1:5, 1000, rep=T) + 5          # (b)
t = c(x, y)                               # (c)
table(t)
t
  1   2   3   4   5   6   7   8   9  10 
222 186 191 205 196 211 223 183 168 215 
z = t[t <= 7]
table(z)                                  # (d)
z
  1   2   3   4   5   6   7 
222 186 191 205 196 211 223 
length(z)
[1] 1434                       # yield: sample of size 1434
chisq.test(tabulate(z))        # passes chi-sq test for uniformity

        Chi-squared test for given probabilities

data:  tabulate(z)
X-squared = 6.2817, df = 6, p-value = 0.3924

If you need specifically $n = 1000$ values:
u = sample(z)                  # scramble order
v = u[1:1000]                  # keep first 1000
length(v)
[1] 1000

table(v)                       
v
  1   2   3   4   5   6   7 
155 123 137 132 139 154 160 

chisq.test(tabulate(v))       # sample of 1000 passes chi-sq test

        Chi-squared test for given probabilities

data:  tabulate(v)
X-squared = 7.888, df = 6, p-value = 0.2464

Note: Another discrete generator in R would be as follows:
a = floor(runif(1000, 1, 6))
table(a)
a
  1   2   3   4   5 
212 214 200 185 189 

A: When you invoke the generator twice then you can generate a random number between 1 and 25. You can use the numbers 1 to 21 to generate your random number from 1 to 7 (use modulo 7), and when you roll 22-25 then you have to try again.
Use the answers from Brain teaser: How to generate 7 integers with equal probability using a biased coin that has a pr(head) = p? to perfect it, that means reduce that loss for the rolls between 22-25. How you have to do this depends on the actual purpose of the random number generator (which changes the costs/benefits). but actually this is a game question, and it would not occur much in real world situations (anyway the rules are not well specified)
