[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Randomly initializing a N-element vector
From: |
David Koelle |
Subject: |
Re: Randomly initializing a N-element vector |
Date: |
Fri, 08 Oct 1999 16:10:56 -0400 |
address@hidden wrote:
> I need to randomly initialize a N-element vector such that
>
> (a) every element is between 0 and 1
> (b) the sum of all elements is 1
> Since this probably is a well-known problem, I'd appreciate any
> pointers, references, etc. on how to handle this.
I would suggest that you start generating a number between 0.0 and 1.0, then
subtract the random value from 1.0 so next time you pick a random number
between 0.0 and 1.0-random, and so on (essentially, to modify Rule B above:
"The difference of all elements from 1 is 0")
x = 1.0
for (i=0; i<n-1; i++) {
statevector[i] = random number between 0.0 and x;
x = x - r;
}
statevector[n] = x;
Hope this helps!
--Dave Koelle
==================================
Swarm-Modelling is for discussion of Simulation and Modelling techniques
esp. using Swarm. For list administration needs (esp. [un]subscribing),
please send a message to <address@hidden> with "help" in the
body of the message.
==================================