Jump to content

Seven-sided dice from five-sided dice: Difference between revisions

m
<lang> needs a language
m (<lang> needs a language)
Line 1:
{{task|Probability and statistics}}
Given an equal-probability generator of one of the integers 1 to 5
as <code>dice5</code>; create <code>dice7</code> that generates a pseudo-random integer from
1 to 7 in equal probability using only <code>dice5</code> as a source of random
numbers, and check the distribution for at least 1000000 calls using the function created in [[Simple Random Distribution Checker]].
 
'''Implementation suggestion:'''
<code>dice7</code> might call <code>dice5</code> twice, re-call if four of the 25
combinations are given, otherwise split the other 21 combinations
into 7 groups of three, and return the group index from the rolls.
Line 12 ⟶ 13:
 
=={{header|Ada}}==
 
The specification of a package Random_57:
 
<lang Ada>package Random_57 is
 
Line 25 ⟶ 24:
 
end Random_57;</lang>
 
Implementation of Random_57:
 
<lang Ada> with Ada.Numerics.Discrete_Random;
 
Line 85 ⟶ 82:
Rand_5.Reset(Gen);
end Random_57;</lang>
 
A main program, using the Random_57 package:
 
<lang Ada>with Ada.Text_IO, Random_57;
 
Line 132 ⟶ 127:
end if;
end Test;
 
 
begin
Line 140 ⟶ 134:
Test(10_000_000, Rand'Access, 0.01);
end R57;</lang>
 
A sample output:
 
<pre>
Sample Size: 10000
Line 170 ⟶ 162:
=={{header|ALGOL 68}}==
{{trans|C}} - note: This specimen retains the original [[Seven-sided dice from five-sided dice#C|C]] coding style.
 
{{works with|ALGOL 68|Revision 1 - no extensions to language used}}
 
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}}
 
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d]}}
 
C's version using no multiplications, divisions, or mod operators:
<lang algol68>PROC dice5 = INT:
Line 265 ⟶ 253:
printf(check(rand7, 7, 1000000, .05) ? "flat\n" : "not flat\n");
return 0;
}</lang>output<pre>flat
<pre>
flat
flat
</pre>
Line 271 ⟶ 261:
=={{header|C++}}==
This solution tries to minimize calls to the underlying d5 by reusing information from earlier calls.
 
<lang cpp>template<typename F> class fivetoseven
{
Line 457 ⟶ 446:
 
=={{header|E}}==
 
{{trans|Common Lisp}}
 
{{improve|E|Write dice7 in a prettier fashion and use the distribution checker once it's been written.}}
 
<lang e>def dice5() {
return entropy.nextInt(5) + 1
Line 471 ⟶ 457:
return d55 %% 7 + 1
}</lang>
 
<lang e>def bins := ([0] * 7).diverge()
for x in 1..1000 {
Line 527 ⟶ 512:
Distribution potentially skewed for bucket 6 Expected: 142857 Actual: 142163
Distribution potentially skewed for bucket 7 Expected: 142857 Actual: 142513</pre>
 
=={{header|Go}}==
<lang go>package main
Line 591 ⟶ 577:
 
=={{header|Groovy}}==
Solution:
<lang groovy>random = new Random()
 
Line 605 ⟶ 590:
(raw % 7) + 1
}</lang>
 
Test:
<lang groovy>def test = {
Line 633 ⟶ 617:
test(it)
}</lang>
 
Output:
<pre style="height:30ex;overflow:scroll;">TRIAL #1
Line 766 ⟶ 749:
 
=={{header|Icon}} and {{header|Unicon}}==
 
{{trans|Ruby}}
 
Uses <code>verify_uniform</code> from [[Simple_Random_Distribution_Checker#Icon_and_Unicon|here]].
 
<lang Icon>
$include "distribution-checker.icn"
Line 810 ⟶ 790:
 
getD7=: groupin3s@keepGood@toBase10@roll2xD5</lang>
 
Here are a couple of variations on the theme that achieve the same result:
<lang j>getD7b=: 0 8 -.~ 3 >.@%~ 5 #. [: <:@rollD5 2 ,~ ]
getD7c=: [: (#~ 7&>:) 3 >.@%~ [: 5&#.&.:<:@rollD5 ] , 2:</lang>
 
The trouble is that we probably don't have enough D7 rolls yet because we compressed out any double D5 rolls that evaluated to 21 or more. So we need to accumulate some more D7 rolls until we have enough. J has two types of verb definition - tacit (arguments not referenced) and explicit (more conventional function definitions) illustrated below:
 
Line 826 ⟶ 804:
y $ rolls NB. shape the result according to the vector y
)</lang>
 
Here's a tacit definition that does the same thing:
<lang j>getNumRolls=: [: >. 0.75 * */@[ NB. calc approx 3/4 of the required rolls
Line 892 ⟶ 869:
 
=={{header|Lua}}==
 
<lang lua>dice5 = function() return math.random(5) end
 
Line 904 ⟶ 880:
<lang Mathematica>sevenFrom5Dice := (tmp$ = 5*RandomInteger[{1, 5}] + RandomInteger[{1, 5}] - 6;
If [tmp$ < 21, 1 + Mod[tmp$ , 7], sevenFrom5Dice])</lang>
 
<pre>CheckDistribution[sevenFrom5Dice, 1000000, 5]
->Expected: 142857., Generated :{142206,142590,142650,142693,142730,143475,143656}
Line 939 ⟶ 914:
=={{header|Perl 6}}==
{{works with|Rakudo Star|2010.09}}
<p>
Since rakudo is still pretty slow, we've done some interesting bits of optimization.
We factor out the range object construction so that it doesn't have to be recreated each time, and we sneakily <em>subtract</em> the 1's from the 5's, which takes us back to 0 based without having to subtract 6.
Line 962 ⟶ 936:
}</lang>
And the output:
<langpre>Expect 142857.143
1 142835 -0.02%
2 143021 +0.11%
Line 969 ⟶ 943:
5 143258 +0.28%
6 142485 -0.26%
7 142924 +0.05%</langpre>
 
=={{header|PicoLisp}}==
Line 1,019 ⟶ 993:
 
=={{header|REXX}}==
<lang rexx>/*REXX program to simulate 7-sided die base on a 5-sided throw. */
<lang rexx>
/*REXX program to simulate 7-sided die base on a 5-sided throw. */
 
arg trials samp .
if trails=='' then trials=1
if samp=='' then samp=1000000
 
 
do t=1 for trials
Line 1,051 ⟶ 1,023:
end /*t*/
</lang>
Output when the following input is specified as <tt>11</tt>:
<br><br>
11
<pre style="height:30ex;overflow:scroll">
───────────────trial: 1 1000000 samples, expect=142857
Line 1,164 ⟶ 1,134:
side 6 had 142971 occurances difference from expected= 114
side 7 had 142800 occurances difference from expected= -57
 
</pre>
 
Line 1,204 ⟶ 1,173:
=={{header|Ruby}}==
{{trans|Tcl}}
 
Uses <code>distcheck</code> from [[Simple_Random_Distribution_Checker#Ruby|here]].
<lang ruby>require './distcheck.rb'
Line 1,257 ⟶ 1,225:
loop until j < 21
dice7 = j mod 7 + 1
end function</lang>
</lang>
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.