code: def _capped_random_choice(x, size, replace: bool = True):
      """
      if replace==True
          randomly chooses from x `size` elements without replacement if len(x)>size
          else allows replacement and selects `size` elements again.
      if replace==False
          randomly chooses from x `min(len(x), size)` elements without replacement
      """
      len_x = x if isinstance(x, int) else len(x)
      if replace:
          return np.random.choice(x, size=size, replace=len_x < size)
      else:
          return np.random.choice(
              x, size=min(size, len_x), replace=False
          )
  
file_path: pytorch3d/pytorch3d/implicitron/dataset/scene_batch_sampler.py
imports: import warnings
  from collections import Counter
  from dataclasses import dataclass, field
  from typing import Dict, Iterable, Iterator, List, Sequence, Tuple
  import numpy
  from torch.utils.data.sampler import Sampler
  from .dataset_base import DatasetBase
label_desc: Use image node_id 2 for calling a global function with example usage: _capped_random_choice(x, size, replace) and returns: np, np