| Bytes | Lang | Time | Link |
|---|---|---|---|
| 094 | Java 8 | 171110T151020Z | Kevin Cr |
| 013 | 05AB1E | 180809T134633Z | Kevin Cr |
| 081 | Python | 240429T160002Z | movatica |
| 053 | Perl 5 | 240429T151218Z | Xcali |
| 012 | Vyxal | 240429T020802Z | emanresu |
| 058 | MATLAB | 180808T171526Z | aaaaa sa |
| 052 | R | 131217T172304Z | Sven Hoh |
| 067 | R | 180808T124545Z | JayCe |
| 071 | ><> | 151005T133645Z | Aaron |
| 016 | GS2 | 151005T061816Z | recursiv |
| 034 | TIBASIC | 150704T184402Z | lirtosia |
| 026 | GolfScript | 131217T152443Z | Howard |
| 028 | GolfScript | 110220T000709Z | Joey |
| 071 | QBasic | 110219T020053Z | PleaseSt |
| 030 | J | 110215T233507Z | J B |
| 044 | Ruby | 110218T231246Z | steensla |
| nan | 110218T123906Z | Lars Hau | |
| nan | 110215T234554Z | Eelvex | |
| 045 | Windows PowerShell | 110218T105723Z | Joey |
| 087 | Bash | 110216T102622Z | Peter Ta |
| 096 | Bash with only one loop | 110216T120743Z | Peter Ta |
| nan | 110216T014317Z | Aman Zee | |
| 071 | JavaScript | 110215T231226Z | PleaseSt |
Java 8, 130 95 94 bytes
v->{long d=9,p,r=0;for(;r<1e9;r=r*10+d--)for(p=d;p==d++|p+d==7;d*=Math.random())d=6;return r;}
-1 byte thanks to @emanresuA.
As full program with verbose main-method and printing the dice-rolls directly would be 178 132 bytes instead (for explanation, see edit history of 95 bytes version):
interface M{static void main(String[]a){for(int d=9,p,i=10;i-->0;System.out.print(d--))for(p=d;p==d++|p+d==7;d*=Math.random())d=6;}}
Explanation:
v->{ // Method with empty unused parameter and long return-type
long d=9, // Random dice integer, starting at 9
// (initially, any value outside range [0,6] for `d` is fine)
p, // Previous dice-roll, starting uninitialized
r=0; // Result-long, starting at 0
for(;r<1e9 // Loop as long as the result is not 10 digits yet:
; // After every iteration:
r= // Update the result to:
r*10+d // Multiply by 10 and add the dice-roll
// (aka, append the dice-roll to the result-long)
--) // And decrease `d` by 1 afterwards with `d--`
for(p=d; // Set `p` to the current `d`
p==d // Inner loop as long as `p` is still equal to `d`
++ // And increase `d` by 1 afterwards with `d++`
| // Or
p+d==7 // As long as `p` and `d` combined equals 7
// (aka `p` and `d` are at opposite sides of the dice)
d*=Math.random())d=6;}
// Do a random 'dice roll' in the range [0,6)
// (which is why we've did `d++` in the loop-condition)
return r;} // After the loops, return the result-long
05AB1E, 23 13 bytes
TF6LsD7α‚KΩD?
If outputting newline-delimited instead of concatenated is allowed, it could be 1 byte less by replacing the D? with =:
Try it online.
Explanation:
TF # Loop 10 times:
6L # Push list [1,2,3,4,5,6]
s # Swap the top two values on the stack (in the first iteration,
# it'll push an additional copy of the list)
D # Duplicate this value/list
7α # Get the absolute difference with 7
‚ # Pair the two together
K # Remove those two values from the list
# (pop + no-op for the pair of lists in the first iteration)
Ω # Pop and push a random item from this list
D? # Duplicate, pop and output it without newline
Python, 87 81 bytes
-6 bytes by emanresu A
from random import*
p=0
for _ in'x'*10:print(p:=choice([*{1,2,3,4,5,6}-{p,7-p}]))
Vyxal, 12 bytes
₀(:7ε"6ɾ$F℅…
₀( # Ten times
… # Print
℅ # A random item of
6ɾ # [1...6]
$F # Without
:7ε" # [prev,7-prev] (stack is padded with zeros)
MATLAB 58 bytes
a=randi(6)
for i=1:9;b=1:6;b([a,7-a])=[];a=b(randi(4))
end
R, 56 52
for(i in 0:9)cat(F<-sample(setdiff(1:6,c(F,7-F)),1))
R, 67 bytes
c(3,5,1,4,2,6)[(sample(1:6,1)+cumsum(sample((-2:2)[-3],9,T)))%%6+1]
There is a golfier R answer but this I think is a different approach from the answers submitted so far.
c(3,5,1,4,2,6) #A dice and its facets
(sample(1:6,1) #Initial dice roll
+cumsum(sample((-2:2)[-3],9,T))) #9 tippings in c(-2,-1,1,2)
%%6+1 #converts to values in [0,6]
[ ]#
><>, 71 bytes
I'm glad I could showcase ><>'s x code-pointer randomization as I don't recall seeing it here.
a&0 v
/2v
1x3v
>x< <<
6x4v
\5v ~
:{:/ ^?=}
:{:/ ^?=7+}
:~$<^&;!?:-1&n
You can try it on this online interpreter (paste code, submit, start).
GS2, 16 bytes
16 2f 25 08 41 20 17 30 16 2f 31 31 25 09 19 32
Here's how it works
16 2f 25 # make range from 1 to 6 and push random element
08 # start block
41 # duplicate top of stack twice
20 17 30 # negate top of stack and add 7
16 2f # push range from 1 to 6
31 31 # do set-wise difference with each of the two previous numbers
25 # push a random element from the list
09 # end block
19 32 # repeat block 9 times
TI-BASIC, 38 34
For(I,1,9
Ans→X
Repeat Ans≠X and Ans≠7-X
randInt(1,6
End
Disp Ans
End
Boring solution, but it's shorter than the previous revision. I take advantage of the fact that on a fresh calculator, Ans is initialized to zero.
GolfScript, 26 chars
0{(.6,5@--\-.,rand=).}10*;
A slight more compressed version of Joey's, basically working around the issue with zero-indexing.
GolfScript, 28
0:|;{7,[0|7|-]-.,rand=:|}10*
QBasic (71 characters)
The two newlines are necessary and included in the character count as one character each.
RANDOMIZE:FOR I=0TO 9
1N=INT(RND*6)+1:IF L=N OR L+N=7THEN 1
?N:L=N:NEXT
J, 30 characters
>:(?@4:{(i.6)-.],5&-)^:(<10)?6
6 2 3 5 4 2 4 1 3 6
Explanations (read from right to left):
?6returns a random number between 0 and 5^:(<10)applies a function 9 times, accumulating the results along the way. The function is:?@4:{(i.6)-.],5&-] , 5&-returns an array of the input number and its complement to 5 (we're handling 0-based numbers currently, so the sum of opposite faces is 5)(i. 6) -.removes them from the full set of integers 0 to 5. We're left with all valid positions after a single tipping operation from the input position.?@4: {picks one of those at random.
>:increments the whole sequence to bring the figures back to the 1 to 6 interval.
Ruby, 44
c=0;10.times{$><<c=([*1..6]-[c,7-c]).sample}
I found the [*1..6] trick by lucky experimenting.
Ruby
66 characters
(0..9).reduce([]){|m|m<<((1..6).to_a-[d=m[-1]||0,7-d]).shuffle[0]}
J
This should work but unfortunately J's random generator gets stuck after the 3rd iteration:
a=:>:i.6
f=:a#~1-(+&(a=])7&-)
((,(?4)&{@f@(_1&{))^:9)>:?6
6 4 5 4 5 4 5 4 5 4
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.
Bash: 97 94 92 90 89 87
Heavily golfed from Aman ZeeK Verma's answer:
for((i=10,f=0;i--;))do for((n=f;n==f||n+f==7;f=RANDOM%6+1))do : done printf $f done
NB arguably it can be shrunk by 5 chars by changing the first line to for((;i++<10;)) but that makes assumptions which aren't always valid. It would work ok in ideone but someone running it from a shell could have i or f exported to something non-zero.
Bash with only one loop: 100 99 98 96
for((i=10,f=RANDOM%6+1;i--;))do printf $f ((n=RANDOM%4+1,m=f<4?f:7-f,f=n<m||++n<7-m?n:n+1)) done
The key idea is that to pick a random number in [1,x] which isn't equal to y you can pick a random number in [1,x-1] and then increment if it's >= y. For this problem we want a random number in [1,6] which isn't equal to f or 7-f. We have to do the two tests in order min(f,7-f), max(f,7-f).
Assuming an initially empty environment could save 2 chars by not initialising i and changing the loop condition to i++<10
Bash
#/!bin/bash
f=`expr $RANDOM % 6`
f=`expr $f + 1`
printf "$f"
for ((i=0; i<9; i++))
do
((bad=7-$f))
next=`expr $RANDOM % 6`
next=`expr $next + 1`
while [ $next -eq $bad ] || [ $next -eq $f ]
do
next=`expr $RANDOM % 6`
next=`expr $next + 1`
done
printf "$next"
f=$next
done
sample code: http://ideone.com/CCfro
JavaScript (71 characters)
You may need to replace print with alert or something else, depending on your JavaScript environment.
for(C=L=T=0;C++<10;print(L=T))while(!(T-L&&T+L-7))T=Math.random()*6+1|0