End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
class method
OpenSSL::BN.rand
BN.rand(bits [, fill [, odd]]) → aBN
ext/openssl/ossl_bn.c
View on GitHub ↗
static VALUE
ossl_bn_s_rand(int argc, VALUE *argv, VALUE klass)
{
BIGNUM *result;
int bottom = 0, top = 0, b;
VALUE bits, fill, odd, obj;
switch (rb_scan_args(argc, argv, "12", &bits, &fill, &odd)) {
case 3:
bottom = (odd == Qtrue) ? 1 : 0;
/* FALLTHROUGH */
case 2:
top = NUM2INT(fill);
}
b = NUM2INT(bits);
obj = NewBN(klass);
if (!(result = BN_new())) {
ossl_raise(eBNError, "BN_new");
}
if (BN_rand(result, b, top, bottom) <= 0) {
BN_free(result);
ossl_raise(eBNError, "BN_rand");
}
SetBN(obj, result);
return obj;
}
Generates a cryptographically strong pseudo-random number of bits.
See also the man page BN_rand(3).