How to similuate a coin flip with probablility p
이전 댓글 표시
How do I simulate getting a result, either 0 or 1, with probability p. So if p=0.5 I should get an output of 0 half of the time, and 1 half of the time.
댓글 수: 1
ali fadi
2022년 4월 11일
probability and statics the probability function flipping 3 coins in matlab
채택된 답변
추가 답변 (3개)
Nick
2011년 11월 9일
Alternatively you could use the randi function in MATLAB which generates random integers.
100 tosses with 0=heads, 1=tails
coin=randi([0:1], [100,1])
It should more or less give you 50 0's and 50 1's.
If there is more than 2 possible outcomes and they all occur with the same probability then just increase the integer range of the randi function.
댓글 수: 4
Evangelia Lo
2021년 11월 13일
What if i want a program whick calculate how many times it will give 0000000000 (10×0)like that in a row ...How can i do it ?
Evangelia Lo
2021년 11월 13일
Which *
Image Analyst
2021년 11월 13일
편집: Image Analyst
2021년 11월 13일
@Evangelia Lo, if you have the Image Processing Toolbox you can use bwareafilt() to extract runs of only the length you want and then bwlabel() or regionprops() to count them. If you want to find the index where each run starts, use regionprops():
coins = logical(randi([0, 1], 1, 30000)); % 30,000 coins
patternLength = 10; % Whatever you want
% Throw out all regions that don't match the length we want using bwareafilt(). Need to invert.
coins2 = bwareafilt(~coins, [patternLength, patternLength]);
% Count them
props = regionprops(coins2, 'PixelIdxList');
numPatternOccurrences = numel(props)
% For fun, find out where they occur.
indexes = vertcat(props.PixelIdxList); % All 10 indexes in a run
startingIndex = indexes(1 : patternLength : end) % Just the starting index.
Evangelia Lo
2021년 11월 13일
How can i do it without the Image Processing Toolbox ?
Cory London
2018년 11월 15일
This code is easily minupulated and works pretty well
Ntoss = 100;
x = rand(1, Ntoss); % what is the resulting size of x?
toss = (x < 0.5);
댓글 수: 1
Umar Abdulhamid
2021년 10월 6일
Can you briefly explain this for me please?
SARATHRAJ V
2021년 2월 20일
0 개 추천
Ntoss = 100; x = rand(1, Ntoss); % what is the resulting size of x? toss = (x < 0.5);
카테고리
도움말 센터 및 File Exchange에서 Random Number Generation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!