Advertisement
makispaiktis

Tutorial - Biased coin

Aug 11th, 2021 (edited)
1,130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.38 KB | None | 0 0
  1. clc
  2. clear all
  3.  
  4. % MAIN FUNCTION
  5. p_heads = 0.25;
  6. N = 100;
  7. counter = 0;
  8. for i = 1:N
  9.     face = biasedCoin(p_heads);
  10.     if face == 'heads'
  11.         counter = counter + 1;
  12.     end
  13. end
  14. p_heads
  15. probHeads = counter / N
  16.  
  17. % AUXILIARY FUNCTION
  18. function face = biasedCoin( p_heads )
  19.  
  20.     if rand < p_heads
  21.         face = 'heads';
  22.     else
  23.         face = 'tails';
  24.     end
  25. end
  26.  
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement