1
$\begingroup$

The following code

M= 5/10;
A = ImplicitRegion[0< x < M, {x}];
RandomPoint[A]

gives a random decimal $x\in(0,M)$.

How can I get an exact fraction instead of a decimal? I mean a RandomFraction in $(0,M)$?

PS: Does anyone know why Rationalize[First[RandomPoint[A]]] does not work?

$\endgroup$
2
  • 1
    $\begingroup$ Use a second argument to Rationalize such as Rationalize[First[RandomPoint[A]],0.1] $\endgroup$
    – yarchik
    Jun 3, 2020 at 7:09
  • 1
    $\begingroup$ I'm wondering what you are trying to exactly accomplish. Surely you know that almost all values on the region are not rational, and almost all of rational values would have effectively infinite-length numerator and denominator? Approximate real numbers are about as faithful approximation for these random samples over a contiguous region as any representable number. $\endgroup$
    – kirma
    Jun 3, 2020 at 12:51

1 Answer 1

3
$\begingroup$

I suppose Rationalize[RandomPoint[A]] does not give a fraction because no rational with small denominator is considered to be sufficiently close to a machine precision number. Using the second argument to relax the closeness requirement does the trick.

M = 5/10;
A = ImplicitRegion[0 < x < M, {x}];
Rationalize[RandomPoint[A], .00001]
(*{68/325}*)

If you don't mind fractions featuring large numbers, you can also force a rational approximation by setting the second argument to 0.

Rationalize[RandomPoint[A],0]    
(*{35947131/119638073}*)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.