Is PKCS#7 padding generated with a pseudo-random-number generator okay? Let's take this PHP code. We use libsodium and libsodium-php.
You have $text, $senderPrivateKey, $recipientPublicKey and a $nonce. The latter was obviously created like this by just using Sodium:

$nonce = \Sodium\randombytes_buf($size);


Now this is the code which adds PKCS#7 padding:

/* determine random amount of PKCS7 padding */
$padBytes = mt_rand(1, 255);

/* append padding */
$textBytes .= str_repeat(chr($padBytes), $padBytes);


Afterwards this is encrypted using \Sodium\crypto_box_keypair_from_secretkey_and_publickey and \Sodium\crypto_box.

So is it okay to use mt_rand here? I mean in the PHP documentation it is discouraged to use this for any "cryptographic purposes". However here it is only used for choosing a ("random") character and the length of the padding.

So is this secure? Would using openssl_random_pseudo_bytes(), random_bytes (in PHP 7) or libsodiums \Sodium\randombytes_buf (like it is used for the nonce) be more secure?
And is this a correct implementation of PKCS#7? Especially as the same variable is used for the length and the character of the padding.



Edit: As there was some confusing here some more information: Actually this implementation is only supposed to hide the file size (especially for short pieces of data) a bit to prevent an attacker from guessing the plain-text message. This is implemented in some open-source libraries related to Threema and here is an issue about this.
So basically in the Threema client a cryptographically secure RNG is used where in this example here a unsecure PRNG is used.
And this is what this question is about: Is it less secure to use a PRNG here?
If the intent of the padding is to make a passive attacker learn only a range instead of the exact message length, do not use real_len + rand(max_padlen).
It is more efficient to make the length of the padded message a multiple of a given block size instead.

If your protocol has enough latitude to allow large variations of the padding length, you can use a randomized length, but watch out for replay attacks. If for a given question, the same response is always sent, the question can be sent multiple times, so that the actual response length can be eventually inferred.
In order to mitigate this, the padding length should always be the same for a given question. A simple PRF can be used to achieve this.