Re: How to generate two random numbers satisfying a certain condition
- From: "Peter L. Montgomery" <Peter-Lawrence.Montgomery@xxxxxx>
- Date: Thu, 24 Aug 2006 01:37:28 GMT
In article <12706845.1156365448002.JavaMail.jakarta@xxxxxxxxxxxxxxxxxxxxxx>
Michael Orion <beeworks@xxxxxxxxxxx> writes:
In article
<1156309905.979650.226510@xxxxxxxxxxxxxxxxxxxxxxxxxxxx
"Henri Courant Junior" <ProfoundlyGifted@xxxxxxxxx>
writes:
numbers x and y, x in
I want to (repeatedly) generate a pair of random
the interval [0,1] and y in the interval [0,1], suchthat x*y>c, with c
a fixed constant in the interval [0,1].
(lines deleted)
(Please note thatof x and y and then
the trivial solution of generating pairs of values
discarding the pairs which do not comply with thecondition is not
useful to me because it is too time expensive.)
I ignore the distinction of whether an interval
terval is
open (a, b) or closed [a, b] below.
I suggest you reconsider the trivial method of
hod of generating
pairs (x, y) and rejecting those which don't qualify.
Try something like
do {
x = c + (1 - c)*rand01();
y = c + (1 - c)*rand01();
} while (x*y <= c);
where rand01() returns a random floating point value
in (0, 1).
Before the test x*y <= c, the selected x and y
are uniformly distributed in the square c < x < 1 and
c < y < 1.
Any pair with 0 < x < 1 and 0 < y < 1 and x*y > c
must lie in this square.
We claim the loop is executed at most twice on
ce on average,
for any fixed c. In particular, if (x, y) is in the
half of the square where x + y > c + 1, then
we will have x*y > c. This follows from
x*y = (1 - x)*(1 - y) + (x + y) - 1 > 0 + (c
0 + (c + 1) - 1 = c.
If c and 1-c are known in advance, then each
each iteration
of the loop needs
2 random values in (0, 1)
3 multiplies
2 adds
1 compare
1 branch
Double this to get an upper bound on average time.
Why not replace c with sqrt(c) in the algorithm suggested by Peter. Then every x and y generated lies between sqrt(c) and 1, and satisfies x * y > c.
Also, such a pair would be independent, BUT would be distributed over sqrt(c) to 1, rather than over 0 to 1.
- MO
Let d = 2*sqrt(c).
Select x0, y0 uniformly in square d - 1 <= x0, y0 <= 1. Then
if x0*y0 > c then return (x, y) = (x0, y0);
if (d - x0)*(d - y0) > c then return (x, y) = (d - x0, d - y0);
Otherwise reject (x0, y0).
When justifying that this gives the desired distribution,
we need to show that the two predicates
(*) x0 * y0 > c
(d - x0) * (d - y0) > c
cannot both happen for the same (x0, y0).
If x0*y0 > c, then x0 + y0 > 2*sqrt(c) = d
by the arithmetic-geometric mean inequality.
Whereas (d - x0) * (d - y0) > c implies
(d - x0) + (d - y0) > d and x0 + y0 < d.
The area of the square is (d - 2)^2 = 4 (sqrt(c) - 1)^2;
The area of the piece where c < x < 1
and c < y < 1 and x*y > c is 1 - c + c*ln(c).
The square has two such pieces, so the probability
that at least one inequality in (*) holds is
1 - c + c*ln(c)
-----------------
2 (sqrt(c) - 1)^2
This is close to 1 - (1 - c)/6 - (1 - c)^2/16 for c near 1.
For c = 0.5, our estimate is that one iteration suffices
with probability about 1 - 1/12 - 1/64 ~= 0.901.
For c near 0, this non-rejection probability is about 0.5.
The average number of iterations is the reciprocal of this probability.
--
If a wall separating US and Mexico is to be effective,
it must stop the killer bees.
pmontgom@xxxxxx Microsoft Research and CWI Home: Bellevue, WA
.
- References:
- Re: How to generate two random numbers satisfying a certain condition
- From: Peter L. Montgomery
- Re: How to generate two random numbers satisfying a certain condition
- From: Michael Orion
- Re: How to generate two random numbers satisfying a certain condition
- Prev by Date: Re: An uncountable countable set
- Next by Date: Re: An uncountable countable set
- Previous by thread: Re: How to generate two random numbers satisfying a certain condition
- Next by thread: Re: How to generate two random numbers satisfying a certain condition
- Index(es):
Relevant Pages
|
Loading