code: def randomly_bin(N, ndim=None, ones=True, exact=True):
      """
      generate N bins randomly gridded across ndim dimensions
  
      Inputs:
          N  --  integer number of bins, where N = prod(bins)
          ndim  --  integer length of bins, thus ndim = len(bins)
          ones  --  if False, prevent bins from containing "1s", wherever possible
          exact  --  if False, find N-1 bins for prime numbers
      """
      if ndim == 0:
          return []
      if N == 0:
          return [0] if ndim else [0] * ndim
      from itertools import chain
      from mystic.tools import random_state
  
      random = random_state().random
  
      def factors(n):
          result = list()
          for i in chain([2], range(3, n + 1, 2)):
              s = 0
              while n % i == 0:
                  n //= i
                  s += 1
              result.extend([i] * s)
              if n == 1:
                  return result
  
      result = factors(N)
      dim = nfact = len(result)
      prime = nfact == 1
      if ndim:
          result += [1] * (ndim - (nfact // ndim))
          dim = ndim
      elif ones:
          result += [1]  # add some 'randomness' by adding a "1"
      # if ones, mix in the 1s; otherwise, only use 1s when ndim < len(result)
      if ones:
          result = sorted(result, key=lambda v: random())
      else:
          result[:nfact] = sorted(
              result[:nfact], key=lambda v: random()
          )
      from numpy import prod as product
  
      result = [product(result[i::dim]) for i in range(dim)]
      # if not ones, now needs a full sort to sort in the 1s
      if not ones:
          result = sorted(result, key=lambda v: random())
      elif not ndim and 1 in result:
          result.remove(1)  # remove the added "1"
      # if it's a prime, then do N-1 if exact=False
      if not exact and N > 3 and prime:
          result = randomly_bin(N - 1, ndim, ones)
      return result
  
label_desc: Use image node_id 4 for calling a global function with example usage: randomly_bin(N, ndim, ones, exact) and returns: result, list, unknown, result