Two bullet roulette: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|Phix}}: syntax coloured, added expected outcomes)
m (syntax highlighting fixup automation)
Line 56:
{{trans|Go}}
 
<langsyntaxhighlight lang="11l">UInt32 seed = 0
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))</langsyntaxhighlight>
 
{{out}}
Line 130:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">methods =
(
load, spin, load, spin, fire, spin, fire
Line 180:
sixGun[1] := temp
}
}</langsyntaxhighlight>
{{out}}
<pre>55.478% Deaths for : "load, spin, load, spin, fire, spin, fire"
Line 189:
=={{header|C}}==
{{trans|Go}}
<langsyntaxhighlight lang="c">#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
Line 308:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>load, spin, load, spin, fire, spin, fire produces 55.456% deaths.
Line 317:
=={{header|C++}}==
{{trans|C}}
<langsyntaxhighlight lang="cpp">#include <array>
#include <iomanip>
#include <iostream>
Line 444:
 
return 0;
}</langsyntaxhighlight>
{{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 ]</langsyntaxhighlight>
 
=={{header|Factor}}==
{{trans|Julia}}
{{trans|Python}}
<langsyntaxhighlight lang="factor">USING: accessors assocs circular formatting fry kernel literals
math random sequences ;
IN: rosetta-code.roulette
Line 556:
} [ run-test ] assoc-each ;
 
MAIN: main</langsyntaxhighlight>
{{out}}
<pre>"rosetta-code.roulette" run</pre>
Line 569:
{{trans|Wren}}
Though procedural rather than OO.
<langsyntaxhighlight lang="go">package main
 
import (
Line 658:
fmt.Printf("%-40s produces %6.3f%% deaths.\n", mstring(m), pc)
}
}</langsyntaxhighlight>
 
{{out}}
Line 670:
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">
let Pistol = function(method) {
this.fired = false;
Line 722:
console.log();
}
</syntaxhighlight>
</lang>
{{out}}
Example:
Line 743:
=={{header|Julia}}==
{{trans|Python}}
<langsyntaxhighlight lang="julia">const cyl = zeros(Bool, 6)
 
function load()
Line 796:
 
testmethods()
</langsyntaxhighlight>{{out}}
<pre>
Method load, spin, load, spin, fire, spin, fire produces 55.54253 per cent deaths.
Line 806:
=={{header|Kotlin}}==
{{trans|C}}
<langsyntaxhighlight lang="scala">import kotlin.random.Random
 
val cylinder = Array(6) { false }
Line 907:
test("LLSFSF");
test("LLSFF");
}</langsyntaxhighlight>
{{out}}
<pre>load, spin, load, spin, fire, spin, fire produces 55.638% deaths.
Line 915:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[Unload, Load, Spin, Fire]
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]</langsyntaxhighlight>
{{out}}
<pre>0.55243
Line 1,010:
=={{header|Nim}}==
 
<langsyntaxhighlight Nimlang="nim">import algorithm, random, sequtils, strformat, strutils, tables
 
type
Line 1,053:
randomize()
for scenario in ["LSLSFSF", "LSLSFF", "LLSFSF", "LLSFF"]:
test(scenario)</langsyntaxhighlight>
 
{{out}}
Line 1,063:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 1,114:
$total += &$ref for 1..$trials;
printf "%7s %.2f%%\n", $ref, $total / $trials * 100;
}</langsyntaxhighlight>
{{out}}
<pre>LSLSFSF 55.04%
Line 1,122:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,180:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">""" Russian roulette problem """
import numpy as np
 
Line 1,274:
percentage = 100 * sum([method() for _ in range(TESTCOUNT)]) / TESTCOUNT
print("Method", name, "produces", percentage, "per cent deaths.")
</langsyntaxhighlight>{{out}}
<pre>
Method load, spin, load, spin, fire, spin, fire produces 55.652 per cent deaths.
Line 1,284:
=={{header|Raku}}==
 
<syntaxhighlight lang="raku" perl6line>unit sub MAIN ($shots = 6);
 
my @cyl;
Line 1,337:
 
say "{.fmt('%7s')}: %{(%revolver{$_} / $trials × 100).fmt('%.2f')}"
for <LSLSFSF LSLSFF LLSFSF LLSFF></langsyntaxhighlight>
{{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 &nbsp; '''200%''' &nbsp; faster.
<langsyntaxhighlight lang="rexx">/*REXX pgm simulates scenarios for a two─bullet Russian roulette game with a 6 cyl. gun.*/
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)</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs, &nbsp; showing that 2<sup>nd</sup> option &nbsp; '''B''' &nbsp; has the highest probability for a suicide:}}
<pre>
Line 1,417:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "random" for Random
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)
}</langsyntaxhighlight>
 
{{out}}
10,327

edits