swarm-modeling
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Randomly initializing a N-element vector


From: jalex
Subject: Randomly initializing a N-element vector
Date: Fri, 8 Oct 1999 12:53:33 -0700 (PDT)

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

I find that uniformly sampling the space carved out by those
requirements a bit tricky.  The first thing I tried was to generate a
vector in the N-dimensional cube---since it is easy to do so without
bias---and then throw the vector away if it doesn't fall within the
space I'm interested in.  I.e., something like:

    float sum;
    int i;
    BOOL in_simplex = NO;
    do {
      sum = 0.0;
      for (i=0; i<11; i++) {
        initialStateVector[i] = 
          [uniformDblRand getDoubleWithMin: 0.0 withMax: 1.0];
        sum += initialStateVector[i];
      }

      if (sum <= 1.0) in_simplex = YES;
    } while (in_simplex == NO);

Of course, if I had thought for a little longer before hammering out
this code, I would have realized that the space I'm interested in
takes up so little volume in the N-dimensional cube that I'll have to
wait too long to get a valid point. 

Since this probably is a well-known problem, I'd appreciate any
pointers, references, etc. on how to handle this.  


Thanks,

Jason Alexander
Logic & Philosophy of Science
University of California, Irvine


                  ==================================
   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.
                  ==================================


reply via email to

[Prev in Thread] Current Thread [Next in Thread]