Skip to content

Commit f6ad1b3

Browse files
authored
Add Random.next_bool and .next_int (#16297)
First step for internalizing `Random::DEFAULT`.
1 parent bf6dba5 commit f6ad1b3

File tree

5 files changed

+18
-4
lines changed

5 files changed

+18
-4
lines changed

samples/sdl/tv.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class ColorMaker
6464
end
6565

6666
def make_alpha_color(multiplier)
67-
rand = Random::DEFAULT.next_int
67+
rand = Random.next_int
6868
r = ((rand >> 16) % 256).to_i
6969
g = ((rand >> 8) % 256).to_i
7070
b = (rand % 256).to_i

spec/std/random_spec.cr

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,11 @@ describe "Random" do
196196
end
197197

198198
it "gets a random bool" do
199-
Random::DEFAULT.next_bool.should be_a(Bool)
199+
Random.next_bool.should be_a(Bool)
200+
end
201+
202+
it "gets a random int" do
203+
Random.next_int.should be_a(Int32)
200204
end
201205

202206
it "generates by accumulation" do

src/colorize.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
# ```
9292
# require "colorize"
9393
#
94-
# "foo".colorize(Random::DEFAULT.next_bool ? :green : :default)
94+
# "foo".colorize(Random.next_bool ? :green : :default)
9595
# ```
9696
#
9797
# Available colors are:

src/http/web_socket/protocol.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ class HTTP::WebSocket::Protocol
136136
private def write_payload(data)
137137
return @io.write(data) unless @masked
138138

139-
key = Random::DEFAULT.next_int
139+
key = Random.next_int
140140
mask_array = key.unsafe_as(StaticArray(UInt8, 4))
141141
@io.write mask_array.to_slice
142142

src/random.cr

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,16 @@ module Random
426426
random_bytes(n).hexstring
427427
end
428428

429+
# See `#next_bool`.
430+
def self.next_bool : Bool
431+
DEFAULT.next_bool
432+
end
433+
434+
# See `#next_int`.
435+
def self.next_int : Int32
436+
DEFAULT.next_int
437+
end
438+
429439
# See `#rand`.
430440
def self.rand : Float64
431441
DEFAULT.rand

0 commit comments

Comments
 (0)