On Mon, 15 Jan 2007 05:09:42 -0800, David Kohel <[EMAIL PROTECTED]> 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?

Some ways to get random numbers, beyond what has already been
mentioned in this thread:

 (1) sage: import random
     sage: random.[tab]   # lots of standard functions
     sage: random?
     In particular, random.randint(1,n) gives uniformly distributed
     random integers between 1 and n inclusive.

 (2) import numpy
     numpy.random.[tab]
     Gives a more sophisticated and much more optimized collection of random
     number generators with more distributions.

William

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
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/
-~----------~----~----~----~------~----~------~--~---

Reply via email to