For unique 4 digit number, the following code work fine
function unique_number($length=10) {
$string = '';
// You can define your own characters here.
$characters = "0123456789";
for ($p = 0; $p < $length; $p++) {
$string .= $characters[mt_rand(0, strlen($characters)-1)];
}
return $string;
}
print unique_number(4);
Every plugin has extended capability to change the default value of field or form. You should need to know how this work.
I am giving you the example for ACF custom fields.
//for acf
function my_acf_load_field( $field ) {
$rand = unique_number(4);
$field['default_value'] = $rand;
return $field;
}
add_filter('acf/load_field/name=your_field_name', 'my_acf_load_field');