On Jan 15, 2007, at 8:09 AM, David Kohel wrote:
Any ideas how to get a random number from 1 to n in SAGE?
Here's a bad way to do it:
sage: n = 10^2
sage: G = SymmetricGroup(n)
sage: G.random()(1)
which will blow up as n goes to infinity.
Here's a slightly better one:
sage: x = random()
sage: int(n*x)
At least it gives a float's worth of precision.
Should there be random_integer in addition to random_prime?
You can do
sage: ZZ.random(n)
which will give you something apparently in the range -n <= x <= n.
Or you can do
sage: Integers(n).random_element()
which will give you something in the range 0 <= x <= n-1.
(Except that it seems it has a bug. It is only giving integers in the
range 0 <= x <= n-2. Hmmmm.)
David
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to sage-devel@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://sage.scipy.org/sage/ and http://modular.math.washington.edu/sage/
-~----------~----~----~----~------~----~------~--~---