code: def byte_sample(b: memoryview, size: int, n: int) -> memoryview:
      """Sample a bytestring from many locations
  
      Parameters
      ----------
      b : full memoryview
      size : int
          target size of each sample to collect
          (may be smaller if samples collide)
      n : int
          number of samples to collect
      """
      assert size >= 0 and n >= 0
      if size == 0 or n == 0:
          return memoryview(b"")
  
      parts = []
      max_start = b.nbytes - size
      start = randint(0, max_start)
      for _ in range(n - 1):
          next_start = randint(0, max_start)
          end = min(start + size, next_start)
          parts.append(b[start:end])
          start = next_start
      parts.append(b[start : start + size])
  
      if n == 1:
          return parts[0]
      else:
          return memoryview(b"".join(parts))
  
file_path: distributed/distributed/protocol/compression.py
imports: from __future__ import annotations
  import zlib
  from collections.abc import Callable, Iterable
  from contextlib import suppress
  from functools import partial
  from random import randint
  from typing import TYPE_CHECKING, Any, Literal, NamedTuple
  from packaging.version import parse
  from tlz import identity
  import dask
  from distributed.metrics import context_meter
  from distributed.utils import ensure_memoryview, nbytes
label_desc: Use image node_id 2 for calling a global function with example usage: byte_sample(b, size, n) and returns: memoryview, parts, memoryview