Given a surface Normal vector of an intersection point, how can one generate a random direction vector that is guaranteed to be in the visible hemisphere? The way I am currently doing this is to sample a random direction vector d and dot it with the Normal and keep that vector if the dot product is greater than 0. This method is not guaranteed to generate a vector in the visible hemisphere of BRDF as ~50% of the random vectors would have a 0 or negative dot product result.
I saw somewhere that there is a way to transform the randomly generated vector d to place it in the visible hemisphere using a transformation matrix but I don't know how to do it. Can someone write a [pseudo]code for how one generate a direction vector using the transformation method?