Re: Generate a sequence of random numbers that sum up to 1?
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

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

From: Paul Rubin <//phr.cx@NOSPAM.invalid>
Date: Sun Apr 30 2006 - 01:36:57 CEST

Anthony Liu <antonyliu2002@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
Received on Mon May 1 00:45:42 2006