[random][rational] Is there any standard way to generate a random rationals?
Hi, I’m using Boost.Rational in a simulation program. for some experiment, I want to generate some random values between 2 rational numbers. I’m wondering if someone already worked on this.
AMDG On 03/05/2015 12:12 PM, Damian Vicino wrote:
Hi, I’m using Boost.Rational in a simulation program.
for some experiment, I want to generate some random values between 2 rational numbers. I’m wondering if someone already worked on this.
I'm not aware of anything. You could try something like: - Find a denominator somehow - Then calculate a random numerator In Christ, Steven Watanabe
I was thinking the same approach. But it is necessary to study how the selection affects the distribution. I was hoping someone already made that work :( If I end up doing it, is there any interest in including something like that in the random library?
On Mar 5, 2015, at 8:24 PM, Steven Watanabe
wrote: AMDG
On 03/05/2015 12:12 PM, Damian Vicino wrote:
Hi, I’m using Boost.Rational in a simulation program.
for some experiment, I want to generate some random values between 2 rational numbers. I’m wondering if someone already worked on this.
I'm not aware of anything. You could try something like:
- Find a denominator somehow - Then calculate a random numerator
In Christ, Steven Watanabe
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Il giorno giovedì 5 marzo 2015 22:39:54 UTC+1, Damian Vicino ha scritto:
I was thinking the same approach. But it is necessary to study how the selection affects the distribution.
I don't think there exists such a thing as a "uniformly distributed rational" in some bounded interval; IOW, there's no way of defining a probability measure ( aka a totally additive bounded measure over a sigma algebra of subsets ) over a bounded interval of rationals uniformly with respect to the measure of the corresponding real interval ( indeed, note that the set of rationals in [0,1] has Lebesgue measure zero, that is, the probability that a unfirom real in [0,1] is rational is zero ). It's somewhat like asking for a *uniformly* distributed integer over [0,1,2...,+inf). Of course, this does not mean there isn't a probability distribution of rationals satisfying some special *uniformness* criteria that fits your needs, but not in any *standard* way ....
On 05-03-2015 22:39, Damian Vicino wrote:
I was thinking the same approach. But it is necessary to study how the selection affects the distribution. I was hoping someone already made that work :(
Given a random double in some interval, would it be possible to calculate the nearest rational number? -Thorsten
Il giorno martedì 10 marzo 2015 11:14:26 UTC+1, Thorsten Ottosen ha scritto:
Given a random double in some interval, would it be possible to calculate the nearest rational number?
there's no nearest rational number, as there are inifinetly many of them in any bounded neighbourhood of any real number. Unless by nearest you mean the nearest rational with the greatest representable denominator... but in this case the result will not be uniform anymore. As said, uniformity ( meaning prob(x in [a,b])=b-a ) seems fundamentally impossible for rationals for the aforementioned reasons ...
On Mar 10, 2015, at 11:29 AM, MJanes
wrote: Il giorno martedì 10 marzo 2015 11:14:26 UTC+1, Thorsten Ottosen ha scritto:
Given a random double in some interval, would it be possible to calculate the nearest rational number?
there's no nearest rational number, as there are inifinetly many of them in any bounded neighbourhood of any real number. Unless by nearest you mean the nearest rational with the greatest representable denominator... but in this case the result will not be uniform anymore. As said, uniformity ( meaning prob(x in [a,b])=b-a ) seems fundamentally impossible for rationals for the aforementioned reasons …
Yes, probably for my particular case, the best I can get is a random integer for numerator after fixing the denominator in some number. Making it be in [0-1] will be equivalent to make it between 0 and numerator in that case. Thanks for the comments.
there's no nearest rational number, as there are inifinetly many of them in any bounded neighbourhood of any real number. Unless by nearest you mean the nearest rational with the greatest representable denominator... but in this case the result will not be uniform anymore. As said, uniformity ( meaning prob(x in [a,b])=b-a ) seems fundamentally impossible for rationals for the aforementioned reasons ...
That not withstanding, the conversion from floating point to rational is *exact*. So floating point values in the range [1/INT_MAX, INT_MAX] would map exactly to rational values in that range. Whether they have the distribution you want is another matter. John.
participants (5)
-
Damian Vicino
-
John Maddock
-
MJanes
-
Steven Watanabe
-
Thorsten Ottosen