code: def test_categorical_generator():
      # This test fails with dtype float16 if the probabilities themselves cannot be
      # well-represented in float16.  When the float16 random picks are assigned to buckets,
      # only certain bucket-probabilities are possible.  Here we map the desired probabilites
      # (e.g. 0.1) to nearby float16 probabilities (e.g. 0.10009766) that are achievable.
      def quantize_probs(probs, dtype):
          if dtype == "float16":
              # float16 has a 10-bit fraction plus an implicit leading 1, so all probabilities
              # of the form N/2^11 (where N is an integer) are representable.
              num_quanta = 2048.0
              quantized_probs = (
                  np.rint(np.array(probs) * num_quanta) / num_quanta
              )
              # Ensure probabilities add to 1
              quantized_probs[0] += 1.0 - quantized_probs.sum()
          else:
              # no need to quantize probs with this data precision
              quantized_probs = np.array(probs)
          return quantized_probs
  
      ctx = mx.context.current_context()
      probs = [0.1, 0.2, 0.3, 0.05, 0.15, 0.2]
      samples = 1000000
      trials = 5
      buckets = list(range(6))
      for dtype in ["float16", "float32", "float64"]:
          quantized_probs = quantize_probs(probs, dtype)
          generator_mx = lambda x: mx.nd.random.categorical(
              data=mx.nd.array(quantized_probs, ctx=ctx, dtype=dtype),
              shape=x,
          ).asnumpy()
          # success_rate was set to 0.15 since PR #13498 and became flaky
          # both of previous issues(#14457, #14158) failed with success_rate 0.25
          # In func verify_generator inside test_utilis.py
          # it raise the error when success_num(1) < nrepeat(5) * success_rate(0.25)
          # by changing the 0.25 -> 0.2 solve these edge case but still have strictness
          verify_generator(
              generator=generator_mx,
              buckets=buckets,
              probs=quantized_probs,
              nsamples=samples,
              nrepeat=trials,
              success_rate=0.20,
          )
          generator_mx_same_seed = lambda x: np.concatenate(
              [
                  mx.nd.random.categorical(
                      data=mx.nd.array(
                          quantized_probs, ctx=ctx, dtype=dtype
                      ),
                      shape=x // 10,
                  ).asnumpy()
                  for _ in range(10)
              ]
          )
          verify_generator(
              generator=generator_mx_same_seed,
              buckets=buckets,
              probs=quantized_probs,
              nsamples=samples,
              nrepeat=trials,
              success_rate=0.20,
          )
  
imports: import os
  import math
  import itertools
  import mxnet
  from mxnet.test_utils import verify_generator, gen_buckets_probs_with_ppf, assert_almost_equal
  import numpy
  import random
  from common import retry, random_seed
  import scipy.stats
  import unittest
  import pytest
  from mxnet.test_utils import *
  from mxnet.base import MXNetError
  from common import assertRaises
label_desc: Use image node_id 19 for calling a global function with example usage: test_categorical_generator() and returns: quantized_probs