Some handy GW-BASIC or QBASIC functions


/ Published in: Other
Save to your folder(s)



Copy this code and paste it in your HTML
  1. 'returns a random integer less than A and greater than or equal to 0
  2. DEF FNRN(A)=INT(RND(1)*A)
  3.  
  4. 'rolls a die (singular of dice)
  5. DEF FNDI=FNRN(6)+1
  6.  
  7. 'returns true at the specified probability, otherwise false
  8. 'FNPR(.7) is true 70% of the time
  9. DEF FNPR(P)=P>RND(1)
  10.  
  11. 'flips a coin
  12. DEF FNFC=FNPR(.5)
  13.  
  14. 'Like mod but works correctly for negative values of A
  15. 'Useful for Pac-Man-style screen wrapping and many other things
  16. DEF FNWR(A,B)=A-INT(A/B)*B
  17.  
  18. 'hypotenuse
  19. DEF FNHY(A,B)=SQR(A*A+B*B)
  20.  
  21. 'distance between 2 points in 2 dimensions
  22. DEF FND2(X1,Y1,X2,Y2)=SQR((X1-X2)^2+(Y1-Y2)^2)
  23.  
  24. 'distance between 2 points in 3 dimensions
  25. DEF FND3(X1,Y1,Z1,X2,Y2,Z2)=SQR((X1-X2)^2+(Y1-Y2)^2+(Z1-Z2)^2)
  26.  
  27. 'volume of a sphere
  28. DEF FNSV(R)=4.1888*R^3
  29.  
  30. 'surface area of a sphere
  31. DEF FNSA(R)=12.5664*R^2

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.