Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support negative argument in (Secure)Random.random_number #2374

Merged
merged 2 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions lib/securerandom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ Value SecureRandom_random_number(Env *env, Value self, Args &&args, Block *) {
} else {
if (arg->is_float()) {
double max = arg->as_float()->to_double();
if (max <= 0) {
env->raise("ArgumentError", "invalid argument - {}", arg->inspect_str(env));
}
if (max <= 0)
max = 1.0;
return generate_random(0.0, max);
} else if (arg->is_range()) {
Value min = arg->as_range()->begin();
Expand Down Expand Up @@ -67,9 +66,8 @@ Value SecureRandom_random_number(Env *env, Value self, Args &&args, Block *) {
}

nat_int_t max = IntegerObject::convert_to_nat_int_t(env, arg);
if (max <= 0) {
env->raise("ArgumentError", "invalid argument - {}", arg->inspect_str(env));
}
if (max <= 0)
return generate_random(0.0, 1.0);
return generate_random(0, max - 1);
}
}
Expand Down
20 changes: 8 additions & 12 deletions spec/library/securerandom/random_number_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,17 @@
end

it "generates a random float number between 0.0 and 1.0 if argument is negative" do
NATFIXME 'it generates a random float number between 0.0 and 1.0 if argument is negative', exception: ArgumentError, message: 'invalid argument - -10' do
num = SecureRandom.random_number(-10)
num.should be_kind_of(Float)
0.0.should <= num
num.should < 1.0
end
num = SecureRandom.random_number(-10)
num.should be_kind_of(Float)
0.0.should <= num
num.should < 1.0
end

it "generates a random float number between 0.0 and 1.0 if argument is negative float" do
NATFIXME 'it generates a random float number between 0.0 and 1.0 if argument is negative float', exception: ArgumentError, message: 'invalid argument - -11.1' do
num = SecureRandom.random_number(-11.1)
num.should be_kind_of(Float)
0.0.should <= num
num.should < 1.0
end
num = SecureRandom.random_number(-11.1)
num.should be_kind_of(Float)
0.0.should <= num
num.should < 1.0
end

it "generates different float numbers with subsequent invocations" do
Expand Down
20 changes: 14 additions & 6 deletions src/random.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
class Random
class << self
def rand(*args)
DEFAULT.rand(*args)
def rand(...)
DEFAULT.rand(...)
end
alias random_number rand

def bytes(*args)
Random.new.bytes(*args)
def random_number(...)
DEFAULT.random_number(...)
end

def bytes(...)
Random.new.bytes(...)
end
end

alias random_number rand
def random_number(*args)
if args.size == 1 && args[0].is_a?(Numeric) && args[0].negative?
args[0] = 1.0
end
rand(*args)
end
end
16 changes: 16 additions & 0 deletions test/natalie/random_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,20 @@
it 'should extend Random::Formatter' do
Random.should be_kind_of(Random::Formatter)
end

describe 'Random.random_number' do
it "generates a random float number between 0.0 and 1.0 if argument is negative" do
num = Random.random_number(-10)
num.should be_kind_of(Float)
0.0.should <= num
num.should < 1.0
end

it "generates a random float number between 0.0 and 1.0 if argument is negative float" do
num = Random.random_number(-11.1)
num.should be_kind_of(Float)
0.0.should <= num
num.should < 1.0
end
end
end
Loading