Windows PowerShell, 45
-join(0..9|%{($d=1..6-ne(7-$d)-ne$d|random)})
Pretty trivial, actually. I generate a list of possible dice rolls 1..6
and then select only those not equal to seven minus the last roll and then only those not equal to the last roll. From the remaining list I then select a random item and assign it to $d
. Since $d
is initially treated as 0
it rolls a normal die the first time.
Test script:
for($i=0;$i-lt20;$i++){
$o=@(./tipping.ps1)
if ($i-gt0-and$o-eq$o2) { throw "Must have random output" }
if ($o.count-ne1) { throw "Must only have one line of output" }
if ($o[0]-match'[^1-6]'){ throw "Invalid characters" }
if($o[0].length-ne10){ throw "Wrong length: $($o[0].length)" }
$r=[char[]]($o[0])|%{$_-48}
for ($x=1;$x-lt$r.count;$x++){
if ($r[$x-1]+$r[$x]-eq7) { throw "Not a tipping: $($r[$x-1]) and $($r[$x])" }
}
$o2=$o
}
History:
- 2011-02-18 11:57 (61) First attempt.
- 2011-02-18 11:58 (45) I don't need to generate the first number separately.
1Some nice tricks here, great stuff. Hitting myself on the head for missing the Array#sample method. – Lars Haugseth – 2011-02-21T08:49:16.757