function randomstate = Generate(q) % This function is used in the chapter on Markov Chain, weather condition % extracts one of the three states {1-clear, 2-cloudy, 3-rainy} from a distribution of % discrete probability (vector q) of these states. % x = rand(1,1); randomstate = 1; u = q(1); while u < x randomstate = randomstate + 1; u = u + q(randomstate); end end