Last active
August 29, 2015 14:20
-
-
Save yutaono/9bca8e5c27018389a88c to your computer and use it in GitHub Desktop.
Marcov chain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait State { | |
import State._ | |
val seq: Seq[(Double, State)] | |
def next: State = | |
(seq sortWith { (s1, s2) => | |
(s1._1 * randValue) > (s2._1 * randValue) | |
}).head._2 | |
} | |
case object A extends State { | |
override lazy val seq: Seq[(Double, State)] = Seq(0.7 -> A, 0.3 -> B) | |
} | |
case object B extends State { | |
override lazy val seq: Seq[(Double, State)] = Seq(0.8 -> A, 0.2 -> B) | |
} | |
object State { | |
val rand = new scala.util.Random | |
def randValue = rand.nextDouble | |
} | |
def rec(st: State, c: Int = 0): Int = { | |
if (c == 100) 0 | |
else { | |
val next = st.next | |
println(s"$c : $st -> $next") | |
rec(next, c + 1) | |
} | |
} | |
rec(A) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment