code: def rand_sparse(
      rng, nse=0.5, post=lambda x: x, rand_method=jtu.rand_default
  ):
      def _rand_sparse(shape, dtype, nse=nse):
          rand = rand_method(rng)
          size = math.prod(shape)
          if 0 <= nse < 1:
              nse = nse * size
          nse = min(size, int(nse))
          M = rand(shape, dtype)
          indices = rng.choice(size, size - nse, replace=False)
          M.flat[indices] = 0
          return post(M)
  
      return _rand_sparse
  
imports: from functools import partial
  import math
  import operator
  from absl.testing import absltest
  from absl.testing import parameterized
  import numpy
  import jax
  from jax import config, jit, lax
  import jax.numpy
  import jax._src.test_util
  from jax.experimental.sparse import BCOO, BCSR, sparsify, todense, SparseTracer
  from jax.experimental.sparse.transform import arrays_to_spvalues, spvalues_to_arrays, sparsify_raw, SparsifyValue, SparsifyEnv
  from jax.experimental.sparse.util import CuSparseEfficiencyWarning
  from jax.experimental.sparse import test_util
label_desc: Use image node_id 1 for calling a global function with example usage: rand_sparse(rng, nse, post, rand_method) and returns: _rand_sparse, post