Generate a sequence of random numbers that sum up to 1?

Paul Rubin http
Sat Apr 29 19:36:57 EDT 2006


Anthony Liu <antonyliu2002 at yahoo.com> writes:
> OK, I actually just want to "manually" create Hidden
> Markov Models by randomly generating the initial state
> probabilities PI,

OK, this sounds like you actually want to divide the unit interval up
into pieces of varying sizes.  Example (untested):

  n = 10   # number of pieces desired

  # Take the unit interval [0,1] and sprinkle in n-1 random points
  s = sorted([0,1] + [random() for i in xrange(n-1)])

  # now find the size of each sub-interval and make a list
  r = [(s[i+1] - s[i]) for i in xrange(n)]

  print sum(r)   # should be 1.0 within rounding error



More information about the Python-list mailing list