import math
import numpy as np


def gmm_threshold(n, d, K, gamma, prop=None, sigma=1.0, rng=None):
    """Generate samples from GMM."""
    log_n = math.log(n)
    ThetaSq = 1 + math.sqrt(1 + K * d / (n * log_n))
    ThetaSq *= 4. * log_n * sigma ** 2.
    Theta_min_sq = gamma * ThetaSq

    center = math.sqrt(Theta_min_sq / 2) * np.eye(d)[:K]
    if rng is None:
        rng = np.random.default_rng()

    if prop is None:
        label = rng.integers(K, size=n)
    else:
        label = rng.choice(K, size=n, p=prop)

    X = sigma * rng.normal(size=(n, d)) + center[label]

    return X, label
