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

Content added Content deleted
m (→‎J solution seems ugly: simplify code)
(→‎J solution seems ugly: handle arrays)
Line 11: Line 11:
:You could use this <code>dice5</code> and either <code>dice7a</code> or <code>dice7b</code> for the main bit:
:You could use this <code>dice5</code> and either <code>dice7a</code> or <code>dice7b</code> for the main bit:
<lang j>dice5=: >:@:?@$&5
<lang j>dice5=: >:@:?@$&5
dice7a=: 0 8 -.~ 3 >.@%~ 5 #. [: <:@dice5 2 ,~ ]
dice7a=: 0 8 -.~ 3 >.@%~ 5 #. [: <:@dice5 2 ,~ */
dice7b=: [: (#~ 7&>:) 3 >.@%~ [: 5&#.&.:<:@dice5 ] , 2: </lang>
dice7b=: [: (#~ 7&>:) 3 >.@%~ [: 5&#.&.:<:@dice5 */ , 2: </lang>


:Then it's just a question on ensuring that you've got enough rolls. You could use the following explicit:
:Then it's just a question on ensuring that you've got enough rolls. You could use the following explicit:
Line 18: Line 18:
dice7=: monad define
dice7=: monad define
res=. 0$0
res=. 0$0
while. y > #res do.
while. (*/y) > #res do.
res=. res, dice7a >. 0.75 * y
res=. res, dice7a >. 0.75 * y
end.
end.
y {. res
y $ res
)
)
</lang>
</lang>
Line 28: Line 28:
:Here is a more instructive version of the main bit of code:
:Here is a more instructive version of the main bit of code:
<lang j>
<lang j>
dice5=: [: >: ] ?@$ 5: NB. makes a y shape array of 5s, "rolls" the array and increments.
dice5=: [: >: ] ?@$ 5: NB. makes a y shape array of 5s, "rolls" the array and increments.
rolltwice=: [: dice5 2 ,~ ] NB. rolls dice5 twice for each desired dice7 roll (y rows, 2 cols)
rolltwice=: [: dice5 2 ,~ */ NB. rolls dice5 twice for each desired dice7 roll (*/y rows, 2 cols)
base5to10=: 5 #. <: NB. decrements and converts rows from base 5 to 10
base5to10=: 5 #. <: NB. decrements and converts rows from base 5 to 10
keepgood=: #~ 21&> NB. compress out values not less than 21
keepgood=: #~ 21&> NB. compress out values not less than 21
groupsof3=: [: >. >: % 3: NB. increments, divides by 3 and takes ceiling
groupsof3=: [: >. >: % 3: NB. increments, divides by 3 and takes ceiling


dice7c=: groupsof3@keepgood@base5to10@rolltwice
dice7c=: groupsof3@keepgood@base5to10@rolltwice