Markov chain
in
Share your Work
•
2 years ago
Hi girls,
I just made a small script that simulate a Markov chain.
Here it is
And for those who have difficulties to figure out how it works, here is a picture ;)
Feel free to optimize the code!
I just made a small script that simulate a Markov chain.
Here it is
- // by Ignivome
- // ignivome@gmail.com
- /*int[][] markov = {
- {60, 40}, //state 0 (total 100%)
- {95, 5} //state 1 (total 100%)
- };*/
-
- int[][] markov = {
- {60, 25, 15}, //state 0 (total 100%)
- {25, 50, 25}, //state 1 (total 100%)
- {35, 25, 40} //state 2 (total 100%)
- };
-
- int state = 0; //markov state
- void setup() {}
- void draw() {}
- void mousePressed() {
- changeMarkovState();
- }
- void changeMarkovState() {
- float rnd = random(100.0); //rnd 100 for a 100% range
- float range = 0;
-
- for(int i = 0; i < markov[0].length; i++) {
- range += float(markov[state][i]);
- if(rnd <= range) {
- state = i; //set the new state
- break;
- }
- }
- println("Current state: " + state);
- }
And for those who have difficulties to figure out how it works, here is a picture ;)
Feel free to optimize the code!
1