monte carlo random number generation

조회 수: 21 (최근 30일)
stavros boutsikos
stavros boutsikos 2021년 3월 29일
댓글: stavros boutsikos 2021년 3월 29일
i want to use a function that generates a random number from [1,2,3,4,5,6,7,8,9,10] but the probabilities of each number are different. For exaample the pof drawing one is 0.2 but p of drawing 3 is 0.06? is there a function or a code that i can use in order to generate a random number by taking into consideration that each number has different weighting?

채택된 답변

Andreas Apostolatos
Andreas Apostolatos 2021년 3월 29일
Hello,
It seems that what you are seeking for can be utilized using function 'randsample' when using the variant that takes as argument variable 'population', which in your case is vector '[1,2,3,4,5,6,7,8,9,10]', and an additional vector of weights 'w' that has the same size as variable 'population', see the following documentation link for more information,
Note that function 'randsample' is using a uniform probability distribution and it is provided as part of the Statistics and Machine Learning Toolbox. The topic is also discussed in the following MATLAB Answers thread where additional workflows are discussed,
I hope that this information helps.
Kind Regards,
Andreas
  댓글 수: 1
stavros boutsikos
stavros boutsikos 2021년 3월 29일
thank you very much Andrea, it was really helpfull and it helped me realise that my code was working and i was just overthinking it.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2021년 3월 29일
You can simply get the cumulative sum and compare your random number to that. For example
r = rand
c = cumsum(p)
output = find(r < c, 1, 'last')
If that doesn't work for you, then give us all the probability values.
  댓글 수: 1
stavros boutsikos
stavros boutsikos 2021년 3월 29일
thank you very much, it actually helped me understand that the code i had was actually working!

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by