uuid
- 1_8_6_287
- 1_8_7_72
- 1_8_7_330
- 1_9_1_378
- 1_9_2_180
- 1_9_3_125
- 1_9_3_392
- 2_1_10
- 2_2_9
- 2_4_6 (0)
- 2_5_5 (-38)
- 2_6_3 (1)
- What's this?
uuid()
public
# SecureRandom.random_number generates a random number. # # If a positive integer is given as n, # SecureRandom.random_number returns an integer, such that: # +0 <= SecureRandom.random_number(n) < n+. # # p SecureRandom.random_number(100) #=> 15 # p SecureRandom.random_number(100) #=> 88 # # If 0 is given or an argument is not given, # SecureRandom.random_number returns a float, such that: # +0.0 <= SecureRandom.random_number() < 1.0+. # # p SecureRandom.random_number #=> 0.596506046187744 # p SecureRandom.random_number #=> 0.350621695741409 # def random_number(n=0)
if 0 < n if defined? OpenSSL::BN OpenSSL::BN.rand_range(n).to_i else hex = n.to_s(16) hex = '0' + hex if (hex.length & 1) == 1 bin = [hex].pack("H*") mask = bin[0].ord mask |= mask >> 1 mask |= mask >> 2 mask |= mask >> 4 begin rnd = random_bytes(bin.length) rnd[0] = (rnd[0].ord & mask).chr end until rnd < bin rnd.unpack("H*")[0].hex end else # assumption: Float::MANT_DIG <= 64 if defined? OpenSSL::BN i64 = OpenSSL::BN.rand(64, -1).to_i else i64 = random_bytes(8).unpack("Q")[0] end Math.ldexp(i64 >> (64-Float::MANT_DIG), -Float::MANT_DIG) end
end
# SecureRandom.uuid generates a random v4 UUID (Universally Unique IDentifier). # # p SecureRandom.uuid #=> “2d931510-d99f-494a-8c67-87feb05e1594” # p SecureRandom.uuid #=> “bad85eb9-0713-4da7-8d36-07a8e4b00eab” # p SecureRandom.uuid #=> “62936e70-1815-439b-bf89-8492855a7e6b” # # The version 4 UUID is purely random (except the version). # It doesn’t contain meaningful information such as MAC addresses, timestamps, etc. # # The result contains 122 random bits (15.25 random bytes). # # See RFC 4122 for details of UUID. #