code: def _random_choice(xp, a, size, p):
      try:
          return xp.random.choice(a, size, p=p)
      except ValueError:
          # Validate the sum of the probabilities as NumPy PR #6131 (numpy>=1.10)
          tol = xp.finfo(p.dtype).eps ** 0.5
          p = p.astype(xp.float64)
          xp.testing.assert_allclose(p.sum(), 1, rtol=0, atol=tol)
  
          # Normalize the probabilities as they satisfy the validation above, and
          # generate samples again
          p /= p.sum()
          return xp.random.choice(a, size, p=p)
  
imports: import chainer
  from chainer.backends import cuda
  from chainer import distribution
  from chainer.functions.math import exponential
  import chainer.functions.math.sum
  from chainer.utils import cache
label_desc: Use image node_id 2 for calling a global function with example usage: _random_choice(xp, a, size, p) and returns: xp, xp