Advertisement
Sorceress

Math: Random Points

Mar 5th, 2014
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. Some useful functions, for generating uniformly distributed random points
  2. by @_sorceress, March 2014
  3. =========================================================================
  4.  
  5. - All functions input a radius 'r'.
  6. - 'random' should return a uniformly distributed random number between 0 and 1.
  7.  
  8. ---------------------------------------------------------------------
  9. Point (x,y) on the circle perimeter
  10.  
  11. a = 2*pi*random
  12.  
  13. x = r*cos(a)
  14. y = r*sin(a)
  15. ---------------------------------------------------------------------
  16. Point (x,y) within the circular disk
  17.  
  18. a = 2*pi*random
  19. b = sqrt(random)*r
  20.  
  21. x = b*cos(a)
  22. y = b*sin(a)
  23. ---------------------------------------------------------------------
  24. Point (x,y) within the annulus, with internal radius m*r (where 0<m<1)
  25.  
  26. a = 2*pi*random
  27. b = m*m
  28. b = r*sqrt(b+random*(1-b))
  29.  
  30. x = b*cos(a)
  31. y = b*sin(a)
  32. ---------------------------------------------------------------------
  33. Point (x,y,z) within the cylinder, with height h
  34.  
  35. a = 2*pi*random
  36. b = sqrt(random)*r
  37.  
  38. x = b*cos(a)
  39. y = b*sin(a)
  40. z = h*random
  41. ---------------------------------------------------------------------
  42. Point (x,y,z) on the spherical surface
  43.  
  44. a = 2*random-1
  45. b = 2*pi*random
  46. c = r*sqrt(1-a*a)
  47.  
  48. x = c*cos(b)
  49. y = c*sin(b)
  50. z = r*a
  51. ---------------------------------------------------------------------
  52. Point (x,y,z) within the spherical ball
  53.  
  54. d = r*random^(1/3)
  55. a = 2*random-1
  56. b = 2*pi*random
  57. c = d*sqrt(1-a*a)
  58.  
  59. x = c*cos(b)
  60. y = c*sin(b)
  61. z = d*a
  62. ---------------------------------------------------------------------
  63. Point (x,y,z) within the spherical ball (generally faster, but unpredictable halting time)
  64.  
  65. do {
  66. x = r*(2*random-1)
  67. y = r*(2*random-1)
  68. z = r*(2*random-1)
  69. } while(x*x+y*y+z*z>r*r)
  70. ---------------------------------------------------------------------
  71. Point (x,y,z) within the thick spherical shell, with inner radius m*r (where 0<m<1)
  72.  
  73. d = m*m*m
  74. d = r*(d+random*(1-d))^(1/3)
  75. a = 2*random-1
  76. b = 2*pi*random
  77. c = d*sqrt(1-a*a)
  78.  
  79. x = c*cos(b)
  80. y = c*sin(b)
  81. z = d*a
  82. ---------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement