In order for the question to make sense, it must hold that $$ \mu_1 + \cdots + \mu_M = 1. $$ This is called a _convex combination_ , in contrast to a general linear combination.
Anyway, generating a sample for $X$ is simple: first pick the number $i$ with probability $\mu_i$ for $i=1,\ldots,M$, then if $i$ was chosen, set $X=Y_i$.
You can see why this works by looking at the _law of total probability_ , which states that $$ P\left(B\right) = \sum_i P\left(B\mid A_i\right) P\left(A_i\right) $$ whenever $A_1,A_2,\ldots$ are disjoint events with positive probability such that $$ P\left(A_1\right) + P\left(A_2\right) + \cdots = 1.$$