Advertisement
felixnardella

estimation of pi using monte carlo simulation

Mar 15th, 2023 (edited)
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | Source Code | 0 0
  1. 5 rem estimation of pi using monte carlo simulation
  2. 10 rem implemented in basic v2 for c64 by felice nardella
  3. 15 t=ti
  4. 20 poke53280,11:s=8192
  5. 25 poke53272,peek(53272)or8:poke53265,peek(53265)or32
  6. 30 gosub220
  7. 35 rem clear color ram
  8. 40 cb$="pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp"
  9. 45 cb$=cb$+cb$:cb$=cb$+left$(cb$,121)
  10. 50 print"{clear}"cb$cb$cb$cb$;:print"ppp";:poke2023,16
  11. 55 tw(0)=1:fori=1to7:tw(i)=tw(i-1)*2:next
  12. 60 rem fori=1024to2023:pokei,208:next
  13. 65 goto95
  14. 70 rem plot x,y
  15. 75 p=s+320*int(y/8)+8*int(x/8)+(yand7)
  16. 80 pokep,peek(p)ortw(7-(xand7))
  17. 85 return
  18. 90 rem plot square
  19. 95 y=0:forx=0to199:gosub75:next
  20. 100 y=199:forx=0to199:gosub75:next
  21. 105 x=0:fory=0to199:gosub75:next
  22. 110 x=199:fory=0to199:gosub75:next
  23. 115 rem monte carlo
  24. 120 z=rnd(-ti):pn=10000
  25. 125 for i=0 to pn
  26. 130 y=int(rnd(1)*200)
  27. 135 x=int(rnd(0)*200)
  28. 140 if x*x+y*y<40000 then in=in+1:gosub75
  29. 145 next
  30. 150 rem time calc
  31. 155 te=ti-t:poke53280,15
  32. 160 rem wait for a key
  33. 165 poke198,0:wait198,1
  34. 170 gosub220
  35. 175 rem turn graphics off
  36. 180 printchr$(147)"total dots:"pn
  37. 185 print"internal dots:"in:print"external dots:"pn-in:print
  38. 190 print"estimated pi ="4*in/pn:print
  39. 195 print"total time:"te/3600"min"
  40. 200 poke53265,peek(53265)and(223)
  41. 205 poke53272,21:poke53280,14
  42. 210 end
  43. 215 rem clear graphic ram
  44. 220 cb$=""
  45. 225 p1=peek(51):p2=peek(52)
  46. 230 poke51,64:poke52,63
  47. 235 forcn=1to125:cb$=cb$+chr$(0):next
  48. 240 poke51,p1:poke52,p2
  49. 245 return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement