# how to distribute probability randomly for generating test data in matlab?

<>This question already has an answer here:

- Generating a probability distribution 6 answers

## Answers

You can just divide the vector by the sum of it's elements. For example, for a vector of length 4 you can do:

>> v = rand(4, 1); >> v = v/sum(v) v = 0.2951 0.3281 0.0460 0.3308 >> sum(v) ans = 1.0000

Note, I am assuming you want uniformly distributed numbers, since you don't state what distribution you want in the question.

Depending on how random you need to be, Roger Stafford takes a more stringent approach.