Given a random number generator r()
which produces a (pseudo-)random double
in the interval [0,1]
with uniform density, ie p(x) = 1
for 0 <= x <= 1
and p(x) = 0
elsewhere, create a random number generator r(a,b)
which generates a double
in the interval [a,b]
with density p(x) = 1/(b-a)
for a <= x <= b
.
Asked
Active
Viewed 1,047 times
1

bountiful
- 814
- 1
- 8
- 22
-
possible duplicate of [Generating Uniform Random Deviates within a given range](http://stackoverflow.com/questions/446153/generating-uniform-random-deviates-within-a-given-range) – Paul R Nov 15 '12 at 22:04
-
The linked question is only considering integers. – bountiful Nov 16 '12 at 09:17
-
Why the elaborate answers? Isn't it as simple as `a + (b - a) * r()`. What am I missing? – Mark Dickinson Nov 16 '12 at 20:02
1 Answers
-1
I believe (and have tested) that this produces a uniform distribution.
r(a,b) = ((r * b) mod (b-a)) + a
but is there another more obvious way?
Yes:
r(a, b) = r*(b-a) + a

bountiful
- 814
- 1
- 8
- 22