code: def th_random_choice(a, n_samples=1, replace=True, p=None):
      """
      Parameters
      -----------
      a : 1-D array-like
          If a th.Tensor, a random sample is generated from its elements.
          If an int, the random sample is generated as if a was th.range(n)
      n_samples : int, optional
          Number of samples to draw. Default is None, in which case a
          single value is returned.
      replace : boolean, optional
          Whether the sample is with or without replacement
      p : 1-D array-like, optional
          The probabilities associated with each entry in a.
          If not given the sample assumes a uniform distribution over all
          entries in a.
  
      Returns
      --------
      samples : 1-D ndarray, shape (size,)
          The generated random samples
      """
      if isinstance(a, int):
          a = th.arange(0, a)
  
      if p is None:
          if replace:
              idx = th.floor(th.rand(n_samples) * a.size(0)).long()
          else:
              idx = th.randperm(len(a))[:n_samples]
      else:
          if abs(1.0 - sum(p)) > 1e-3:
              raise ValueError("p must sum to 1.0")
          if not replace:
              raise ValueError(
                  "replace must equal true if probabilities given"
              )
          idx_vec = th.cat(
              [th.zeros(round(p[i] * 1000)) + i for i in range(len(p))]
          )
          idx = (th.floor(th.rand(n_samples) * 999)).long()
          idx = idx_vec[idx].long()
      selection = a[idx]
      if n_samples == 1:
          selection = selection[0]
      return selection
  
imports: import pickle
  import random
  from .optimizers import *
  from .callbacks import *
  import torch
  from pywick.callbacks import *
label_desc: Use image node_id 21 for calling a global function with example usage: th_random_choice(a, n_samples, replace, p) and returns: selection