Skip to content

Instantly share code, notes, and snippets.

@yutaono
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yutaono/9bca8e5c27018389a88c to your computer and use it in GitHub Desktop.
Save yutaono/9bca8e5c27018389a88c to your computer and use it in GitHub Desktop.
Marcov chain
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