keras/src/backend/tensorflow/random.py/beta
def beta(shape, alpha, beta, dtype=None, seed=None):
    dtype = dtype or floatx()
    # since tensorflow doesn't offer a beta distribution function
    # so we'll use the formula U(a,b) = (X(a) / (X(a) + Y(b)),
    # where U(a,b) is a beta-distributed random variable with
    # parameters a and b, and X(a) and Y(b) are gamma-distributed
    # random variables with parameters a and b respectively.

    # Additionally, we'll use two different seeds for our two
    # gamma random variables to prevent any unintended
    # dependencies and correlations between the generated values
    # due to the usage of same seed.
    seed_1 = _cast_seed(draw_seed(seed))
    # The choice of 12 is totally arbitrary, as we're
    # incrementing the first drawn seed by a CONSTANT to
    # ensure deterministic results.
    seed_2 = seed_1 + 12

    # TODO: `tf.random.stateless_gamma` doesn't support bfloat16
    intemediate_dtype = dtype
    if standardize_dtype(dtype) == "bfloat16":
        intemediate_dtype = "float32"
    alpha = tf.convert_to_tensor(alpha, dtype=intemediate_dtype)
    beta = tf.convert_to_tensor(beta, dtype=intemediate_dtype)

    # tensorflow's tf.random.stateless_gamma has a bit of unconventional
    # implementation of the stateless_gamma function where it checks the
    # broadcastability of alpha's shape with ONLY the RIGHTMOST dimension of
    # the specified output shape instead of considering the whole.
    # Consequently, it then results in errors for perfectly broadcastable shapes
    # such as for output shape of (2, 3) and alpha shape of (1, 3)
    # So to resolve this, we explicitly broadcast alpha and beta to shape before
    # passing them to the stateless_gamma function.
    alpha = tf.broadcast_to(alpha, shape)
    beta = tf.broadcast_to(beta, shape)

    gamma_a = tf.cast(
        tf.random.stateless_gamma(
            shape=shape, seed=seed_1, alpha=alpha, dtype=intemediate_dtype
        ),
        dtype,
    )
    gamma_b = tf.cast(
        tf.random.stateless_gamma(
            shape=shape, seed=seed_2, alpha=beta, dtype=intemediate_dtype
        ),
        dtype,
    )
    sample = gamma_a / (gamma_a + gamma_b)
    return sample