import torch


def uniform_random_rot_matrix(num_matrices, std=0.01):
    """
    Uniform sampling of random 3D rotation matrices using QR decomposition.
    Source: https://arxiv.org/pdf/math-ph/0609050.pdf
    """
    Z = torch.randn(num_matrices, 3, 3) * std
    Q, R = torch.qr(Z)
    d = torch.diagonal(R)
    ph = d/torch.abs(d)
    # matmul with diagonal matrix L equivalent to element-wise mul with broad-casted vector l
    Q = torch.mul(Q, ph)
    return Q