Two bullet roulette: Difference between revisions
m
syntax highlighting fixup automation
m (→{{header|Phix}}: syntax coloured, added expected outcomes) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 56:
{{trans|Go}}
<
F nonrandom(n)
:seed = 1664525 * :seed + 1013904223
Line 119:
sum += method(m)
V pc = Float(sum) * 100 / tests
print(‘#<40 produces #2.3% deaths.’.format(mstring(m), pc))</
{{out}}
Line 130:
=={{header|AutoHotkey}}==
<
(
load, spin, load, spin, fire, spin, fire
Line 180:
sixGun[1] := temp
}
}</
{{out}}
<pre>55.478% Deaths for : "load, spin, load, spin, fire, spin, fire"
Line 189:
=={{header|C}}==
{{trans|Go}}
<
#include <stdio.h>
#include <stdlib.h>
Line 308:
return 0;
}</
{{out}}
<pre>load, spin, load, spin, fire, spin, fire produces 55.456% deaths.
Line 317:
=={{header|C++}}==
{{trans|C}}
<
#include <iomanip>
#include <iostream>
Line 444:
return 0;
}</
{{out}}
<pre>load, spin, load, spin, fire, spin, fire produces 55.487% deaths.
Line 453:
=={{header|EasyLang}}==
{{trans|C}}
<syntaxhighlight lang="text">len cyl[] 6
func rshift . .
h = cyl[5]
Line 514:
call test [ 0 1 0 1 2 2 ]
call test [ 0 0 1 2 1 2 ]
call test [ 0 0 1 2 2 ]</
=={{header|Factor}}==
{{trans|Julia}}
{{trans|Python}}
<
math random sequences ;
IN: rosetta-code.roulette
Line 556:
} [ run-test ] assoc-each ;
MAIN: main</
{{out}}
<pre>"rosetta-code.roulette" run</pre>
Line 569:
{{trans|Wren}}
Though procedural rather than OO.
<
import (
Line 658:
fmt.Printf("%-40s produces %6.3f%% deaths.\n", mstring(m), pc)
}
}</
{{out}}
Line 670:
=={{header|JavaScript}}==
<
let Pistol = function(method) {
this.fired = false;
Line 722:
console.log();
}
</syntaxhighlight>
{{out}}
Example:
Line 743:
=={{header|Julia}}==
{{trans|Python}}
<
function load()
Line 796:
testmethods()
</
<pre>
Method load, spin, load, spin, fire, spin, fire produces 55.54253 per cent deaths.
Line 806:
=={{header|Kotlin}}==
{{trans|C}}
<
val cylinder = Array(6) { false }
Line 907:
test("LLSFSF");
test("LLSFF");
}</
{{out}}
<pre>load, spin, load, spin, fire, spin, fire produces 55.638% deaths.
Line 915:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
Unload[] := ConstantArray[False, 6]
Load[state_List] := Module[{s = state},
Line 1,001:
Count[Table[LSLSFF[], n], True]/N[n]
Count[Table[LLSFSF[], n], True]/N[n]
Count[Table[LLSFF[], n], True]/N[n]</
{{out}}
<pre>0.55243
Line 1,010:
=={{header|Nim}}==
<
type
Line 1,053:
randomize()
for scenario in ["LSLSFSF", "LSLSFF", "LLSFSF", "LLSFF"]:
test(scenario)</
{{out}}
Line 1,063:
=={{header|Perl}}==
{{trans|Raku}}
<
use warnings;
use feature 'say';
Line 1,114:
$total += &$ref for 1..$trials;
printf "%7s %.2f%%\n", $ref, $total / $trials * 100;
}</
{{out}}
<pre>LSLSFSF 55.04%
Line 1,122:
=={{header|Phix}}==
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">spin</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">revolver</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">count</span><span style="color: #0000FF;">)</span>
Line 1,169:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Load/Spin/Fire method percentage fatalities:\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">papply</span><span style="color: #0000FF;">({{</span><span style="color: #008000;">"LSLSFSF"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">/</span><span style="color: #000000;">9</span><span style="color: #0000FF;">},{</span><span style="color: #008000;">"LSLSFF"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">/</span><span style="color: #000000;">12</span><span style="color: #0000FF;">},{</span><span style="color: #008000;">"LLSFSF"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">/</span><span style="color: #000000;">9</span><span style="color: #0000FF;">},{</span><span style="color: #008000;">"LLSFF"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">}},</span><span style="color: #000000;">test</span><span style="color: #0000FF;">)</span>
<!--</
{{out}}
<pre>
Line 1,180:
=={{header|Python}}==
<
import numpy as np
Line 1,274:
percentage = 100 * sum([method() for _ in range(TESTCOUNT)]) / TESTCOUNT
print("Method", name, "produces", percentage, "per cent deaths.")
</
<pre>
Method load, spin, load, spin, fire, spin, fire produces 55.652 per cent deaths.
Line 1,284:
=={{header|Raku}}==
<syntaxhighlight lang="raku"
my @cyl;
Line 1,337:
say "{.fmt('%7s')}: %{(%revolver{$_} / $trials × 100).fmt('%.2f')}"
for <LSLSFSF LSLSFF LLSFSF LLSFF></
{{out|Sample output (default; 6 shooter)}}
<pre>LSLSFSF: %55.37
Line 1,373:
Changing the cartridge chamber from an index array to a simple string made the program around '''200%''' faster.
<
parse arg cyls tests seed . /*obtain optional arguments from the CL*/
if cyls=='' | cyls=="," then cyls= 6 /*Not specified? Then use the default.*/
Line 1,406:
if y=='S' then $= $", spin"
if y=='F' then $= $", fire"
end /*a*/; return right( strip( strip($, , ",") ), 45)</
{{out|output|text= when using the default inputs, showing that 2<sup>nd</sup> option '''B''' has the highest probability for a suicide:}}
<pre>
Line 1,417:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<
import "/fmt" for Fmt
Line 1,488:
for (t in 1..tests) sum = sum + rev.method(m)
Fmt.print("$-40s produces $6.3f\% deaths.", Revolver.mstring(m), sum * 100 / tests)
}</
{{out}}
|