# How to get a RandomFraction in (0,M)?

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?

• Use a second argument to Rationalize such as Rationalize[First[RandomPoint[A]],0.1] Jun 3, 2020 at 7:09
• 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. Jun 3, 2020 at 12:51

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}*)