This is pretty simple. Create your module, then create a form "MyRegForm" where the user can input her email and (if needed) password (twice with the '#type' => 'password_confirm'). Then, in the submit() method create your user like this:
\Drupal\user\Entity\User::create([
'name' => 'hGzfT54re39OpLLLk',
'pass' => 'hGzfT54re39OpLLLk',
'mail' => $data->mail,
'status' => 1,
'init' => $data->mail,
'created' => time(),
'changed' => time(),
'login' => 0,
'access' => 0,
])->save();
The change the username and add whatever roles you need with code like
$tuser = user_load_by_name('hGzfT54re39OpLLLk');
$tuser->setUsername('My super name logic');
$tuser->addRole("whatever");
$tuser->save();
$rc = $tuser->id();
If you don't want to create your own form for registration, go for hook_form_alter and set the #access to the "name" field to false and add a #default_value.