Skip to content

Instantly share code, notes, and snippets.

@kenwebb
Last active October 2, 2015 03:18
Show Gist options
  • Save kenwebb/2159078 to your computer and use it in GitHub Desktop.
Save kenwebb/2159078 to your computer and use it in GitHub Desktop.
Coin Flipping Example
<?xml version="1.0" encoding="UTF-8"?>
<!--Xholon Workbook http://www.primordion.com/Xholon/wb/ (C) Ken Webb Sun Oct 27 2013 06:16:36 GMT-0400 (EDT)-->
<XholonWorkbook>
<Notes><![CDATA[
Khan Academy
------------
Title: Coin Flipping Example
Description: u08_l4_t1_we3 Coin Flipping Example
Url: http://www.khanacademy.org/video/coin-flipping-example
InternalName: coin-flipping-example
YoutubeId: 3UlE8gyKbkU
Keywords: u08_l4_t1_we3, Coin, Flipping, Example
params::
param_shouldInsertBehaviors=true
My Notes
--------
Updated Oct 27 2013, with equivalent behaviors for Xholon GWT.
Find the probablity of getting exactly two "heads" when flipping three coins.
There are 8 possible outcomes::
HHH THH
HHT THT
HTH TTH
HTT TTT
this is the sample space
Probability of exactly 2 heads::
there are 3 outcomes with exacly 2 heads - HHT HTH THH
P(Exactly 2 "H") = outcomes with 2 heads / possible outcomes = 3 / 8
where P means "probability"
]]></Notes>
<script implName="lang:python:inline:"><![CDATA[
]]></script>
<script implName="lang:javascript:inline:"><![CDATA[
]]></script>
<_-.XholonClass>
<!-- domain objects -->
<PhysicalSystem/>
<Coin/>
<Coins/>
<Outcomes> <!-- outcomes are the heads and tails that result from flipping coins (ex: HTH) -->
<TotalOutcomes/> <!-- a count of the total number of outcomes (ex: 8) -->
<OutcomesWithTwoHeads/> <!-- a count of the number of outcomes with exaclty two heads (ex: 3) -->
</Outcomes>
</_-.XholonClass>
<xholonClassDetails>
<Coins>
<port name="totalOutcomes" connector="#xpointer(../TotalOutcomes)"/>
<port name="outcomesWithTwoHeads" connector="#xpointer(../OutcomesWithTwoHeads)"/>
</Coins>
</xholonClassDetails>
<PhysicalSystem>
<Coins>
<Coin multiplicity="3"/>
</Coins>
<OutcomesWithTwoHeads>0</OutcomesWithTwoHeads>
<TotalOutcomes>0</TotalOutcomes>
</PhysicalSystem>
<Blockbehavior implName="lang:python:inline:"><![CDATA[
]]></Blockbehavior>
<Blockbehavior implName="lang:javascript:inline:"><![CDATA[
]]></Blockbehavior>
<Coinbehavior implName="lang:webEditionjs:inline:"><![CDATA[
var coinFlip = Math.random() * 2;
if (coinFlip < 1.0) {
print('H');
this.parent().attr('val', 'H');
}
else {
print('T');
this.parent().attr('val', 'T');
}
]]></Coinbehavior>
<Coinsbehavior implName="lang:webEditionjs:inline:"><![CDATA[
this.bindPorts(this.parent());
this.totalOutcomes.incVal(1);
var coins = this.siblings('.Coin');
var heads = coins.filter('[val="H"]');
if (heads.length == 2) {
this.outcomesWithTwoHeads.incVal(1);
}
var probability = this.outcomesWithTwoHeads.attr('val') / this.totalOutcomes.attr('val');
println(" " + probability);
var svg = $($('div#mySVGDiv > object')[0].contentDocument.getElementsByTagNameNS(svgns, 'svg')[0]);
var rectSVG = svg.children('g').children('g').children('rect');
rectSVG.attr('height', probability * 80);
rectSVG.attr('y', 80 - (probability * 80));
]]></Coinsbehavior>
<Coinbehavior implName="org.primordion.xholon.base.Behavior_gwtjs"><![CDATA[
var beh = {
act: function() {
var coin = this.cnode.parent();
var coinFlip = Math.random() * 2;
if (coinFlip < 1.0) {
coin.print('H');
coin.val(1); // 'H'
}
else {
coin.print('T');
coin.val(0); // 'T'
}
}
}
]]></Coinbehavior>
<Coinsbehavior implName="org.primordion.xholon.base.Behavior_gwtjs"><![CDATA[
var coins, rectSVG;
var beh = {
postConfigure: function() {
coins = this.cnode.parent();
},
act: function() {
coins.totalOutcomes.inc(1);
var hcount = 0;
var coin = coins.first();
while (coin != null) {
if (coin.val()) {
hcount++;
}
coin = coin.next();
}
if (hcount == 2) {
coins.outcomesWithTwoHeads.inc(1);
}
var probability = coins.outcomesWithTwoHeads.val() / coins.totalOutcomes.val();
coins.println(" " + probability);
// SVG
if (!rectSVG) {
// SVG setup
var svg = $doc.getElementById("xhsvg").firstElementChild.firstElementChild;
rectSVG = svg.getElementById("PhysicalSystem/OutcomesWithTwoHeads");
}
rectSVG.setAttributeNS(null, 'height', probability * 80);
rectSVG.setAttributeNS(null, 'y', 80 - (probability * 80));
}
}
]]></Coinsbehavior>
<Blockbehavior implName="lang:bsh:inline:"><![CDATA[
]]></Blockbehavior>
<Blockbehavior implName="lang:jruby:inline:"><![CDATA[
]]></Blockbehavior>
<Blockbehavior implName="lang:groovy:inline:"><![CDATA[
]]></Blockbehavior>
<SvgClient><Attribute_String roleName="svgUri"><![CDATA[data:image/svg+xml,
<svg width="100" height="80" xmlns="http://www.w3.org/2000/svg">
<g>
<title>TotalOutcomes</title>
<rect id="PhysicalSystem/TotalOutcomes" fill="#98FB98" height="80" width="50" x="25" y="0"/>
<g>
<title>OutcomesWithTwoHeads</title>
<rect id="PhysicalSystem/OutcomesWithTwoHeads" fill="#6AB06A" height="0.0" width="50" x="25" y="37.5"/>
<line x1="20" y1="50.0" x2="80" y2="50.0" style="stroke:rgb(0,0,255);stroke-width:1"/>
</g>
</g>
</svg>
]]></Attribute_String><Attribute_String roleName="setup">${MODELNAME_DEFAULT},${SVGURI_DEFAULT}</Attribute_String></SvgClient>
</XholonWorkbook>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment