Guest User

Untitled

a guest
Jul 23rd, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. #!/usr/bin/ruby
  2.  
  3.  
  4. def birth
  5. dice = rand(100)
  6. return dice > 50 ? 1 : 0
  7. end
  8.  
  9.  
  10. ### 生了男孩就不生了,生了女孩就一直生
  11. def family_birth(kids=[])
  12. kid = birth
  13. kids.push(kid)
  14. return kids if kid == 1
  15. family_birth(kids) if kid == 0
  16. end
  17.  
  18. ### 家里只准生两个女孩,第三胎是女孩就打掉,是男孩就生
  19. def family_birth_type2(kids=[])
  20. kid = birth
  21. if kid == 1
  22. kids.push(kid)
  23. return kids
  24. elsif kid == 0 and kids.empty?
  25. kids.push(kid)
  26. family_birth_type2(kids)
  27. elsif kid == 0 and kids == [0]
  28. kids.push(kid)
  29. family_birth_type2(kids)
  30. elsif kid == 0 and kids == [0,0]
  31. family_birth_type2(kids)
  32. end
  33. end
  34.  
  35. def bias(n)
  36. kids = []
  37. 1.upto(n) do |i|
  38. family_children = family_birth
  39. kids.push(family_children)
  40. end
  41. return [kids.flatten.count(1),kids.flatten.count(0)]
  42. end
  43.  
  44. def bias_type2(n)
  45. kids = []
  46. 1.upto(n) do |i|
  47. family_children = family_birth_type2
  48. kids.push(family_children)
  49. end
  50. return [kids.flatten.count(1),kids.flatten.count(0)]
  51. end
  52.  
  53. def flat(n)
  54. kids = []
  55. 1.upto(n) do |i|
  56. kids.push(birth)
  57. end
  58. return [kids.count(1),kids.count(0)]
  59. end
  60.  
  61. def plot(start,step,count,func)
  62. i = 0
  63. while i < count - 1
  64. kids = Object.send(func,start + i * step)
  65. p kids[0]/kids[1].to_f
  66. i = i + step
  67. end
  68. end
  69.  
  70. plot(10000,100,50000,'bias_type2')
Add Comment
Please, Sign In to add comment