You need a loot table (archive) and the command
/loot (archive) for this.
Firstly, the loot table that gives (or summons or inserts or whatever) the head of the player it is invoked on.
Minimal file structure of the datapack:
head_loot is an arbitrarily chosen namespace, which is reused in the command later. The same with
head.json, only the file extension needs to be
pack.mcmeta can vary depending on what you want, but this is a minimum example:
Now you can create heads of arbitrary (currently online) players. There are multiple ways to do this, the one that is actually intended for exactly this is:
/execute as @r run loot spawn ~ ~ ~ loot head_loot:head
This runs "as" a random player, but not "at" their position and summons a player head item entity at the execution position (a command block or whatever).
Make sure that the datapack is enabled, this command tells you that:
If you just changed something in the files, use