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