Efficient way of generating a random number of N (less than 64) bits with exactly M bits equal to one
I'd guess that you can simply split this into two problems:


create 64 - n random bits, call this R
shuffle n bits where m bits (at any location) are set to 1, call this P


Finally you can simply perform R | P (presuming big endian notation).

Shuffling lists of elements is an operation present in almost any language. If there is any inefficiency it would be in the shuffling algorithm (although Fisher-Yates is optimal, so you'd expect some form of that algorithm, possibly the inefficiency is getting values in a range...).