title: How can I make several random weights for portfolios?
body: <p>I am learning investment portfolios, but I need to know how I can generate many random weights for portfolios.</p>
  
  <p>To achieve multiply them with the returns of several shares and also obtain volatility and return for each different weights. The weights must add 1 in total, to achieve the results shown in the images.</p>
  
  <p>Here, for example, I generate a weight for the actions of my portfolio, but I need to generate more weights randomly, to simulate more portfolios and achieve the results of the images.</p>
  
  <pre><code>import random
  n=9
  weights = [random.random() for _ in range(n)]
  sum_weights = sum(weights)
  weights = [w/sum_weights for w in weights]
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/MHc7R.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MHc7R.png" alt="image 1"></a>
  <a href="https://i.stack.imgur.com/zDZYx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zDZYx.png" alt="image 2"></a></p>