Markov race

Developer
Size
2,561 Kb
Views
24,288

How do I make an markov race?

Modelling with Markov chains. Read more in post http://codepen.io/potatoDie/post/modelling-randomness-with-markov-chains. What is a markov race? How do you make a markov race? This script and codes were developed by PotatoDie on 15 September 2022, Thursday.

Markov race Previews

Markov race - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Markov race</title>
</head>
<body> <script src="js/index.js"></script>
</body>
</html>

Markov race - Script Codes JS Codes

var ns = "http://www.w3.org/2000/svg";
var markov;
var pSquares = [];
window.onload = function () { // Create svg element var svg = document.createElementNS( ns, "svg"); svg.setAttribute("viewBox", "-6 -6 12 12"); // And add it to the document document.body.appendChild(svg); // Add gfx to the svg // svg.appendChild ( createRect(-1, -1, 2, 2, "#eee") ); // Create 4 empty polar squares var r = 1; var rWidth = 1; var rSpacing = 0.1; for ( var i = 0; i < 4; i++ ) { pSquares[i] = PolarSquare (r, r + rWidth, 0, Math.PI/(i+1)); svg.appendChild(pSquares[i].sprite); r += rWidth + rSpacing; } // Start a Markov process, adding to the length of a ring/square associated // with the current state markov = MarkovProcess(); markov.init(); window.setInterval ( loop, 1000/60);
}
function loop () { var state = markov.step(); pSquares[state].grow();
}
PolarSquare = function ( r0, r1, phi0, phi1 ) { var r0 = r0; var r1 = r1; var phi0 = phi0; var phi1 = phi1; var unit = Math.PI/50; var path = document.createElementNS(ns, "path"); (function init () { path.setAttributeNS(null, "fill", "none"); path.setAttributeNS(null, "stroke", "#000"); path.setAttributeNS(null, "stroke-width", .02); draw(); })(); function toCartesian ( r, phi ) { // Note screen vs math coordinates (that's the reason for the minus sign for phi) // If you choose not to use math coordinates, change the sweep-flag too return [r * Math.cos(phi), r * Math.sin(-phi) ]; } function draw () { function pathData() { var d = "M " + toCartesian (r0, phi0). join(" "); d += [" A", r0, r0, 0, Math.abs(phi1-phi0) > Math.PI ? 1 : 0, 0, toCartesian (r0, phi1)]. join(" "); d += " L " + toCartesian (r1, phi1). join(" "); d += [" A", r1, r1, 0, Math.abs(phi1-phi0) > Math.PI ? 1 : 0, 1, toCartesian (r1, phi0)]. join(" "); d += " Z"; return d; } path.setAttributeNS(null, "d", pathData() ); } return { sprite: path, update: function ( phi ) { phi1 = phi; draw(); }, grow: function () { // Grow the length of the shape with 1 unit // Check phi stays within limits phi1 += unit; phi1 = phi1 % (2 * Math.PI); draw(); } }
}
// Define states and transition matrix
var MarkovProcess = function () { var matrix = [ [0.6, 0.35, 0.02, 0.03], [0.35, 0.6, 0.04, 0.01], [0.01, 0.02, 0.8, 0.17], [0.01, 0.02, 0.17, 0.8] ] var state = 0; var init = function(){ }; var step = function() { // Calculate a new state var distribution = matrix[state]; // get random var r var r = Math.random(); // Check in which area r falls for ( var i = 0; i < distribution.length; i++ ) { if ( r <= distribution[i] ) { // Found it state = i; return state; } else { r -= distribution[i]; } } }; return { init: init, step: step };
};
Markov race - Script Codes
Markov race - Script Codes
Home Page Home
Developer PotatoDie
Username potatoDie
Uploaded September 15, 2022
Rating 3.5
Size 2,561 Kb
Views 24,288
Do you need developer help for Markov race?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

PotatoDie (potatoDie) Script Codes
Create amazing video scripts with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!