Pregunta

How do I generate random numbers using a Markov model in C#? I noticed here that almost all of the applications of the Markov algorithm is for randomly writing text. Is there a source code somewhere or a tutorial where I can fully understand how this works? My goal actually is to generate random numbers to simulate solar energy harvesting.

¿Fue útil?

Solución

First decide how deep your Markov model is going. Do you look at the previous number? The previous two numbers? The previous three numbers? Perhaps deeper?

Second, look through some actual solar energy data and extract the probabilities for what follows a group of 1, 2 or 3 numbers. Ideally you will be able to get complete coverage, but there may well be gaps. For those either extrapolate, or put in some average/random value.

All this so far is data.

Third generate the first 1, 2 or 3 numbers. From your database pick the correct combination and randomly select one of the possible followers. When I do this, I have a low probability random element possible as well so things don't get stuck in a rut.

Drop the earliest element of your 1, 2 or 3 numbers. Shift the others down and add the new number at the end. Repeat until you have enough data.

Here is a short extract from my 1-deep Markov word generator showing part of the data table:

// The line addEntry('h', "e 50 a 23 i 12 o  7 @ 100") shows the the letter
// 'h' is followed by 'e' 50% of the time, 'a' 23% of the time, 'i' 12% of 
// the time, 'o' 7% of the time and otherwise some other letter, '@'.
//
// Figures are taken from Gaines and tweaked. (see 'q')
private void initMarkovTable() {
    mMarkovTable = new HashMap<Character, List<CFPair>>(26);

    addEntry('a', "n 21 t 17 s 12 r 10 l  8 d  5 c  4 m  4 @ 100");
    addEntry('b', "e 34 l 17 u 11 o  9 a  7 y  5 b  4 r  4 @ 100");
    addEntry('c', "h 19 o 19 e 17 a 13 i  7 t  6 r  4 l  4 k  4 @ 100");
    addEntry('d', "e 16 i 14 a 14 o 10 y  8 s  6 u  5 @ 100");
    addEntry('e', "r 15 d 10 s  9 n  8 a  7 t  6 m  5 e  4 c  4 o  4 w 4 @ 100");
    addEntry('f', "t 22 o 21 e 10 i  9 a  7 r  5 f  5 u  4 @ 100");
    addEntry('g', "e 14 h 14 o 12 r 10 a  8 t  6 f  5 w  4 i  4 s  4 @ 100");
    addEntry('h', "e 50 a 23 i 12 o  7 @ 100");
    // ...
}

The data is organised as letter-frequency pairs. I used the '@' character to indicate "pick any letter here". Your data will be number-frequency pairs instead.

To pick an output, I read the appropriate line of the data and generate a random percentage. Scan long the data accumulating the frequencies until the accumulated frequency exceeds the random percantage. That is the letter (or number in your case) you pick.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top