Method: FFaker::UUID#uuidv4

Defined in:
lib/ffaker/uuid.rb

#uuidv4Object

> UUID version 4 is meant for generating UUIDs from truly-random or > pseudo-random numbers.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ffaker/uuid.rb', line 20

def uuidv4
  uuid = 0
  # random_a
  # > The first 48 bits of the layout that can be filled with random data
  # > as specified in Section 6.9. Occupies bits 0 through 47 (octets 0-5).
  uuid |= rand((2**48) - 1) << 80
  # ver
  # > The 4 bit version field as defined by Section 4.2, set to 0b0100 (4).
  # > Occupies bits 48 through 51 of octet 6.
  uuid |= 0b0100 << 76
  # random_b
  # > 12 more bits of the layout that can be filled random data as per
  # > Section 6.9. Occupies bits 52 through 63 (octets 6-7).
  uuid |= rand((2**12) - 1) << 64
  # var
  # > The 2 bit variant field as defined by Section 4.1, set to 0b10.
  # > Occupies bits 64 and 65 of octet 8.
  uuid |= 0b10 << 62
  # random_c
  # > The final 62 bits of the layout immediately following the var field
  # > field to be filled with random data as per Section 6.9. Occupies bits
  # > 66 through 127 (octets 8-15).
  uuid |= rand((2**62) - 1)

  as_string(uuid)
end