Getting random number from Weighted sum of Normal distribution functions [duplicate]

I've a weighted sum of the 2 Gaussian distribution functions as below. How can I get a random number based on this sum of functions. The number of functions can vary up to 10.

Let's say your mixture components have probabilities $$p_1,p_2$$ (add up to $$1$$). A basic algorithm would probably choosing a uniform random variable between $$u\in[0,1]$$ and compare with $$p$$, if it's smaller sample from $$\mathcal{N_1}$$, else sample from $$\mathcal{N_2}$$. For $$K$$ components with probabilities $$p_1,\dots,p_K$$, do the same:
If $$u, sample from $$\mathcal{N_1}$$, else if $$u sample from $$\mathcal{N_2}$$, ..., else if $$u<\sum_{i=1}^m{p_i}$$, sample from $$\mathcal{N_m}$$, which basically applies Inverse Transform Sampling in discrete.