Hi Renato,

On Tue, May 11, 2010 at 9:09 AM, Renato <[email protected]> wrote:

<SNIP>

> In python (I'd like to know it in python, not sage), how can I get an
> ordered array of n random values, all in the interval (a,b), such that
> any two consecutives differ more than h? Of course first thing would be
> checking that b - a > n*h

You could use the Python random package, which has a function called
uniform(a,b) to find a pseudorandom number uniformly distributed
between a and b. Here's an example of what I think you want to do:

[mv...@sage ~]$ sage -ipython
Python 2.6.4 (r264:75706, Mar 31 2010, 08:31:30)
Type "copyright", "credits" or "license" for more information.

IPython 0.9.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object'. ?object also works, ?? prints more.

In [1]: from random import uniform

In [2]: def my_rand(prev, delta, start, end):
   ...:     next = uniform(start, end)
   ...:     while abs(prev - next) <= delta:
   ...:         next = uniform(start, end)
   ...:     return next
   ...:

In [3]: a = -100

In [4]: b = 100

In [5]: d = 3.145

In [6]: n = 10

In [7]: A = [uniform(a, b)]

In [8]: for i in range(n - 1):
   ...:     prev = A[-1]
   ...:     A.append(my_rand(prev, d, a, b))
   ...:
   ...:

In [9]: A
Out[9]:
[-50.561356080352084,
 16.45084767625751,
 44.410150328421139,
 94.061257664210132,
 62.878963867176395,
 13.150190877114596,
 44.929991848023548,
 8.7017092842752959,
 4.4325842110072244,
 -3.1168123962670222]

In [10]: for i in range(len(A) - 1):
   ....:     print A[i], A[i+1], abs(A[i] - A[i+1])
   ....:
   ....:
-50.5613560804 16.4508476763 67.0122037566
16.4508476763 44.4101503284 27.9593026522
44.4101503284 94.0612576642 49.6511073358
94.0612576642 62.8789638672 31.182293797
62.8789638672 13.1501908771 49.7287729901
13.1501908771 44.929991848 31.7798009709
44.929991848 8.70170928428 36.2282825637
8.70170928428 4.43258421101 4.26912507327
4.43258421101 -3.11681239627 7.54939660727

-- 
Regards
Minh Van Nguyen

-- 
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-support
URL: http://www.sagemath.org

Reply via email to