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

Anthony Liu antonyliu2002 at yahoo.com
Sat Apr 22 16:10:46 EDT 2006


Thanks a lot, Alex and Gerard.

I am actually not very concerned about the
inter-dependency of the floating numbers generated
randomly.  They are good enough if they are subject to
the constraint of summing up to 1.  

It is simply not worth the time to get an HMM by
training it on a large corpus. My sole purpose is to
test the predicting power of an HMM, given a set of
parameter values. 

I will definitely try out your snippet and see if it
works.  Thanks a lot!

--- Alex Martelli <aleaxit at yahoo.com> wrote:

> Anthony Liu <antonyliu2002 at yahoo.com> wrote:
>    ...
> > As a matter of fact, given that we have to specify
> the
> > number of states for an HMM, I would like to
> create a
> > specified number of random floating numbers whose
> sum
> > is 1.0.
> 
> def forAL(N):
>      N_randoms = [random.random() for x in
> xrange(N)]
>      total = sum(N_randoms)
>      return [x/total for x in N_randoms]
> 
> 
> Does this do what you want?  Of course, the
> resulting numbers are not
> independent, but then the constraints you pose would
> contradict that.
> 
> 
> Alex
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 



More information about the Python-list mailing list