|
69 | 69 | will take to get there. The classic example of a Las Vegas algorithm is the _quicksort_ sorting
|
70 | 70 | algorithm. The quicksort algorithm will always complete with a fully sorted list, but, the time it
|
71 | 71 | takes to complete is random. Another good example of a Las Vegas algorithm is the code that we use
|
72 |
| -to pick a random point in a unit sphere: |
| 72 | +to pick a random point in a unit disk: |
73 | 73 |
|
74 | 74 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
|
75 |
| - inline vec3 random_in_unit_sphere() { |
| 75 | + inline vec3 random_in_unit_disk() { |
76 | 76 | while (true) {
|
77 |
| - auto p = vec3::random(-1,1); |
| 77 | + auto p = vec3(random_double(-1,1), random_double(-1,1), 0); |
78 | 78 | if (p.length_squared() < 1)
|
79 | 79 | return p;
|
80 | 80 | }
|
81 | 81 | }
|
82 | 82 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
83 | 83 | [Listing [las-vegas-algo]: <kbd>[vec3.h]</kbd> A Las Vegas algorithm]
|
84 | 84 |
|
85 |
| -This code will always eventually arrive at a random point in the unit sphere, but we can't say |
| 85 | +This code will always eventually arrive at a random point in the unit disk, but we can't say |
86 | 86 | beforehand how long it'll take. It may take only 1 iteration, it may take 2, 3, 4, or even longer.
|
87 | 87 | Whereas, a Monte Carlo program will give a statistical estimate of an answer, and this estimate will
|
88 | 88 | get more and more accurate the longer you run it. Which means that at a certain point, we can just
|
|
0 commit comments