class Faker::Code

def generate_imei

def generate_imei
  str = Array.new(15, 0)
  sum = 0
  len = 15
  # Fill in the first two values of the string based with the specified prefix.
  # Reporting Body Identifier list: http://en.wikipedia.org/wiki/Reporting_Body_Identifier
  arr = sample(RBI)
  str[0] = arr[0].to_i
  str[1] = arr[1].to_i
  pos = 2
  # Fill all the remaining numbers except for the last one with random values.
  while pos < (len - 1)
    str[pos] = rand(0..9)
    pos += 1
  end
  # Calculate the Luhn checksum of the values thus far
  len_offset = (len + 1) % 2
  (0..(len - 1)).each do |position|
    if (position + len_offset).odd?
      t = str[position] * 2
      t -= 9 if t > 9
      sum += t
    else
      sum += str[position]
    end
  end
  # Choose the last digit so that it causes the entire string to pass the checksum.
  str[len - 1] = (10 - (sum % 10)) % 10
  # Output the IMEI value.
  str.join
end