select numbers based on different probabiltiies

I have an event that happens at a prbability of 1E-06. I want Matlab to return me 1 if that event happens and 0 otherwise. Rand gives me a number upto four decimal places. Could you folks help me come up with a solution.
Thank you

댓글 수: 4

Bruno Luong
Bruno Luong 2018년 12월 4일
편집: Bruno Luong 2018년 12월 4일
Rand gives me a number upto four decimal places
Nope RAND returns double, i.e., more than 15 significant decimals.
Don't be fool by what display on your screen and what is stored internally.
messi
messi 2018년 12월 4일
Thanks for that quick response. Very basic question. How can I force matlab to output random number upto 6 or 7 significant digit.
Will this code work?
ran = rand;
if (rand<1E-05)
selec=1;
else
selec=0;
end
Image Analyst
Image Analyst 2018년 12월 4일
편집: Image Analyst 2018년 12월 4일
Yes. But you don't need the ran=rand line if you're never going to use ran. To see more digits:
format longG
Bruno Luong
Bruno Luong 2018년 12월 4일
편집: Bruno Luong 2018년 12월 4일
No this code returns 1 with 1e-5 probability chance, not 1e-6 as you have stated.
ran = rand;
if (rand<1E-05)
selec=1;
else
selec=0;
end

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

 채택된 답변

추가 답변 (1개)

카테고리

질문:

2018년 12월 4일

답변:

2018년 12월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by