Skip to content

Listing 37: Incorrectly generating random unit vector #697

@pmcmorris

Description

@pmcmorris

Maybe I'm reading this incorrectly but what the code does doesn't seem to match the function name. The current code appears to do a uniform distribution on cylinder and then stretch to a sphere. Doesn't that raise the probability of points near the poles?

vec3 random_unit_vector() {
    auto a = random_double(0, 2*pi);
    auto z = random_double(-1, 1);  // uniform in z means the same # of hits over less surface area
    auto r = sqrt(1 - z*z);
    return vec3(r*cos(a), r*sin(a), z);
}

I was expecting something more like this:

vec3 random_unit_vector() {
        auto a = random_double(0, 2 * pi);
        auto b = random_double(-pi/2, pi/2);
        auto z = sin(b);
        auto r = sqrt(1 - z * z);
        return vec3(r * cos(a), r * sin(a), z);
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions