0

I want to genearte different probabilities in a range of [0,1]. The only things which changes is the number of the probabilities, so for example in my code below I wanted to generate 4 numbers randomly five times. My main problem is that with my code they don't sum up to 1. How can I achieve this? Thank you.

def generateRandom():
    for i in range (0,5):
        data_uniform = uniform.rvs(size=4, loc = 0.0, scale=1)
        sum_uni=0
        for i in range(0,len(data_uniform)):
            sum_uni+=data_uniform[i]
        print(data_uniform,sum_uni)
MrNobody
  • 535
  • 8
  • 24
  • 2
    After you got your numbers, calculate the sum of these numbers and divide each with that sum. – Niko Föhr Apr 01 '21 at 06:55
  • 2
    https://stackoverflow.com/questions/18659858/generating-a-list-of-random-numbers-summing-to-1#18659948 Does this help? – jda5 Apr 01 '21 at 06:56
  • 1
    Or this : https://stackoverflow.com/questions/44895218/create-an-array-that-sums-to-a-specific-number – arhr Apr 01 '21 at 06:58
  • Thank you guys. @np8 answer worked out for me. Please submit a solution to accept it :) But you can also submit a solution too guys, and I will upvote it because I tried now these solutions too and they also work. – MrNobody Apr 01 '21 at 06:59

0 Answers0