Implement Homestuck's Catenative Doomsday Dice CascaderRoll Dungeons and Dragons diceRole-Playing Game Dice-RollingSimulate a game of CrapsIterated Dice RollingRoll an ASCII dieHow many dice can you roll without rolling the most probable numberFrequency Distribution of Mixed Dice RollsAverage Idle-Dice RollingSimulating Exploding Dice
Hypothesis testing- with normal approximation
Why is macOS limited to 1064 processes?
How do Precipitation Reactions behave in the Absence of Gravity?
We know someone is scrying on us. Is there anything we can do about it?
Did Bercow say the would have sent the EU extension-request letter himself had Johnson not done so?
How to deal with non-stop callers in the service desk
Tikz: Arranging of a Table with Tikz elements
Travel with Expired Greek Passport from UK to Greece and Return
Regarding asking for writing about some courses in my CV in which I got bad grades due to my non interest in those courses
Why the job's next_run_time is wrong?
Why is Iceland Air's Saga Premium product classified as Business class?
iPhone 11 - Leave lock screen after FaceId
Where is the deflector array on the Phoenix?
Adjusting the definition of a well-powered category to category theory with universes: size issues
In Cura, can I make my top and bottom layer be all perimiters?
Why buy a first class ticket on Southern trains?
Light turning on and off
Starting a fire in a cold planet that was full of flammable gas
Can a planet's magnetic field be generated by non-ferromagnetic metals?
Multiple devices with one IPv6 to the Internet?
Surjection from one string to two strings
Why are so many cities in the list of 50 most violent cities in the world located in South and Central America?
What is the difference between an adjective and a noun modifier?
How would a young girl/boy (about 14) who never gets old survive in the 16th century?
Implement Homestuck's Catenative Doomsday Dice Cascader
Roll Dungeons and Dragons diceRole-Playing Game Dice-RollingSimulate a game of CrapsIterated Dice RollingRoll an ASCII dieHow many dice can you roll without rolling the most probable numberFrequency Distribution of Mixed Dice RollsAverage Idle-Dice RollingSimulating Exploding Dice
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;
$begingroup$
Challenge
I'm under attack by the larcenous Midnight Crew and I need to summon the Catenative Doomsday Dice Cascader in order to defend myself. Since I'm low on space, I need the code to be as short as possible.
The algorithm for the Catenative Doomsday Dice Cascader is as follows:
First, the six sided die in the Prime Bubble is rolled, and the result will determine how many iterations of the next step take place.
Start with a six sided die. For as many times as the roll of the Prime Bubble die, multiply the number of sides on the next die by the result of the roll of the current die.
For example, if on your first roll of the six-sided die your roll is 2, then your next die will have 6*2 = 12 sides.
Your goal is to write a function or program that takes no input and outputs the final result of the last die rolled. Since this is code-golf, the lowest byte count in each language wins!
Examples
Example #1 (Taken directly from the link above):
The Prime Bubble rolls a 6, meaning that the Cascader will iterate six times
#1: We always start with a 6 sided die, and it rolls a 2, so the next die has 6x2=12 sides
#2: The 12 sided die rolls an 8, meaning that the third die has 12x8=96 sides
#3: The 96 sided die rolls a 35, meaning that die 4 has 96x35=3360 sides
#4: The 3360 sided die rolls a 2922, so die 5 has 3360x2922 = 9,817,920 sides
#5: The 9.8 million sided die rolls a 5,101,894, so the final die has 50,089,987,140,480 sides
#6: The 50 trillion sided die rolls a one. Hooray.
Since the last die rolled gave a 1, your function or program should output 1.
Example #2
The Prime Bubble rolls a 2, meaning that the Cascader will iterate twice.
#1: We always start with a 6 sided die, and it rolls a 4, so the next die has 6x4 = 24 sides
#2: The 24 sided die rolls a 14
Since the last die rolled gave a 14, your function or program should output 14.
code-golf random
$endgroup$
|
show 7 more comments
$begingroup$
Challenge
I'm under attack by the larcenous Midnight Crew and I need to summon the Catenative Doomsday Dice Cascader in order to defend myself. Since I'm low on space, I need the code to be as short as possible.
The algorithm for the Catenative Doomsday Dice Cascader is as follows:
First, the six sided die in the Prime Bubble is rolled, and the result will determine how many iterations of the next step take place.
Start with a six sided die. For as many times as the roll of the Prime Bubble die, multiply the number of sides on the next die by the result of the roll of the current die.
For example, if on your first roll of the six-sided die your roll is 2, then your next die will have 6*2 = 12 sides.
Your goal is to write a function or program that takes no input and outputs the final result of the last die rolled. Since this is code-golf, the lowest byte count in each language wins!
Examples
Example #1 (Taken directly from the link above):
The Prime Bubble rolls a 6, meaning that the Cascader will iterate six times
#1: We always start with a 6 sided die, and it rolls a 2, so the next die has 6x2=12 sides
#2: The 12 sided die rolls an 8, meaning that the third die has 12x8=96 sides
#3: The 96 sided die rolls a 35, meaning that die 4 has 96x35=3360 sides
#4: The 3360 sided die rolls a 2922, so die 5 has 3360x2922 = 9,817,920 sides
#5: The 9.8 million sided die rolls a 5,101,894, so the final die has 50,089,987,140,480 sides
#6: The 50 trillion sided die rolls a one. Hooray.
Since the last die rolled gave a 1, your function or program should output 1.
Example #2
The Prime Bubble rolls a 2, meaning that the Cascader will iterate twice.
#1: We always start with a 6 sided die, and it rolls a 4, so the next die has 6x4 = 24 sides
#2: The 24 sided die rolls a 14
Since the last die rolled gave a 14, your function or program should output 14.
code-golf random
$endgroup$
4
$begingroup$
Whats the maximum output? If every roll results in the maximum side? I think it's 7958661109946400884391936 = ((((6^2)^2)^2)^2)^2 = 6^(2^5) = 6^32
$endgroup$
– Kjetil S.
Jun 1 at 11:53
6
$begingroup$
@KjetilS. Indeed, and the likelihood of that output should be $frac16*prod_i=0^5(6^2^i)=frac16times 6times 6^2times 6^4times 6^8times 6^16times 6^32=frac12155416739906037495048372267884096782336$
$endgroup$
– Jonathan Allan
Jun 1 at 12:50
3
$begingroup$
Is this supposed to be random? The question doesn't mention anything about randomness at all?
$endgroup$
– Sriotchilism O'Zaic
Jun 1 at 16:35
10
$begingroup$
@SriotchilismO'Zaic Dice-rolling implies randomness.
$endgroup$
– mbomb007
Jun 1 at 21:34
6
$begingroup$
@SriotchilismO'Zaic xkcd.com/221
$endgroup$
– Neyt
Jun 3 at 9:52
|
show 7 more comments
$begingroup$
Challenge
I'm under attack by the larcenous Midnight Crew and I need to summon the Catenative Doomsday Dice Cascader in order to defend myself. Since I'm low on space, I need the code to be as short as possible.
The algorithm for the Catenative Doomsday Dice Cascader is as follows:
First, the six sided die in the Prime Bubble is rolled, and the result will determine how many iterations of the next step take place.
Start with a six sided die. For as many times as the roll of the Prime Bubble die, multiply the number of sides on the next die by the result of the roll of the current die.
For example, if on your first roll of the six-sided die your roll is 2, then your next die will have 6*2 = 12 sides.
Your goal is to write a function or program that takes no input and outputs the final result of the last die rolled. Since this is code-golf, the lowest byte count in each language wins!
Examples
Example #1 (Taken directly from the link above):
The Prime Bubble rolls a 6, meaning that the Cascader will iterate six times
#1: We always start with a 6 sided die, and it rolls a 2, so the next die has 6x2=12 sides
#2: The 12 sided die rolls an 8, meaning that the third die has 12x8=96 sides
#3: The 96 sided die rolls a 35, meaning that die 4 has 96x35=3360 sides
#4: The 3360 sided die rolls a 2922, so die 5 has 3360x2922 = 9,817,920 sides
#5: The 9.8 million sided die rolls a 5,101,894, so the final die has 50,089,987,140,480 sides
#6: The 50 trillion sided die rolls a one. Hooray.
Since the last die rolled gave a 1, your function or program should output 1.
Example #2
The Prime Bubble rolls a 2, meaning that the Cascader will iterate twice.
#1: We always start with a 6 sided die, and it rolls a 4, so the next die has 6x4 = 24 sides
#2: The 24 sided die rolls a 14
Since the last die rolled gave a 14, your function or program should output 14.
code-golf random
$endgroup$
Challenge
I'm under attack by the larcenous Midnight Crew and I need to summon the Catenative Doomsday Dice Cascader in order to defend myself. Since I'm low on space, I need the code to be as short as possible.
The algorithm for the Catenative Doomsday Dice Cascader is as follows:
First, the six sided die in the Prime Bubble is rolled, and the result will determine how many iterations of the next step take place.
Start with a six sided die. For as many times as the roll of the Prime Bubble die, multiply the number of sides on the next die by the result of the roll of the current die.
For example, if on your first roll of the six-sided die your roll is 2, then your next die will have 6*2 = 12 sides.
Your goal is to write a function or program that takes no input and outputs the final result of the last die rolled. Since this is code-golf, the lowest byte count in each language wins!
Examples
Example #1 (Taken directly from the link above):
The Prime Bubble rolls a 6, meaning that the Cascader will iterate six times
#1: We always start with a 6 sided die, and it rolls a 2, so the next die has 6x2=12 sides
#2: The 12 sided die rolls an 8, meaning that the third die has 12x8=96 sides
#3: The 96 sided die rolls a 35, meaning that die 4 has 96x35=3360 sides
#4: The 3360 sided die rolls a 2922, so die 5 has 3360x2922 = 9,817,920 sides
#5: The 9.8 million sided die rolls a 5,101,894, so the final die has 50,089,987,140,480 sides
#6: The 50 trillion sided die rolls a one. Hooray.
Since the last die rolled gave a 1, your function or program should output 1.
Example #2
The Prime Bubble rolls a 2, meaning that the Cascader will iterate twice.
#1: We always start with a 6 sided die, and it rolls a 4, so the next die has 6x4 = 24 sides
#2: The 24 sided die rolls a 14
Since the last die rolled gave a 14, your function or program should output 14.
code-golf random
code-golf random
edited Jun 4 at 11:26
LegionMammal978
15.5k4 gold badges18 silver badges53 bronze badges
15.5k4 gold badges18 silver badges53 bronze badges
asked Jun 1 at 2:48
Bazinga_9000Bazinga_9000
4771 gold badge5 silver badges10 bronze badges
4771 gold badge5 silver badges10 bronze badges
4
$begingroup$
Whats the maximum output? If every roll results in the maximum side? I think it's 7958661109946400884391936 = ((((6^2)^2)^2)^2)^2 = 6^(2^5) = 6^32
$endgroup$
– Kjetil S.
Jun 1 at 11:53
6
$begingroup$
@KjetilS. Indeed, and the likelihood of that output should be $frac16*prod_i=0^5(6^2^i)=frac16times 6times 6^2times 6^4times 6^8times 6^16times 6^32=frac12155416739906037495048372267884096782336$
$endgroup$
– Jonathan Allan
Jun 1 at 12:50
3
$begingroup$
Is this supposed to be random? The question doesn't mention anything about randomness at all?
$endgroup$
– Sriotchilism O'Zaic
Jun 1 at 16:35
10
$begingroup$
@SriotchilismO'Zaic Dice-rolling implies randomness.
$endgroup$
– mbomb007
Jun 1 at 21:34
6
$begingroup$
@SriotchilismO'Zaic xkcd.com/221
$endgroup$
– Neyt
Jun 3 at 9:52
|
show 7 more comments
4
$begingroup$
Whats the maximum output? If every roll results in the maximum side? I think it's 7958661109946400884391936 = ((((6^2)^2)^2)^2)^2 = 6^(2^5) = 6^32
$endgroup$
– Kjetil S.
Jun 1 at 11:53
6
$begingroup$
@KjetilS. Indeed, and the likelihood of that output should be $frac16*prod_i=0^5(6^2^i)=frac16times 6times 6^2times 6^4times 6^8times 6^16times 6^32=frac12155416739906037495048372267884096782336$
$endgroup$
– Jonathan Allan
Jun 1 at 12:50
3
$begingroup$
Is this supposed to be random? The question doesn't mention anything about randomness at all?
$endgroup$
– Sriotchilism O'Zaic
Jun 1 at 16:35
10
$begingroup$
@SriotchilismO'Zaic Dice-rolling implies randomness.
$endgroup$
– mbomb007
Jun 1 at 21:34
6
$begingroup$
@SriotchilismO'Zaic xkcd.com/221
$endgroup$
– Neyt
Jun 3 at 9:52
4
4
$begingroup$
Whats the maximum output? If every roll results in the maximum side? I think it's 7958661109946400884391936 = ((((6^2)^2)^2)^2)^2 = 6^(2^5) = 6^32
$endgroup$
– Kjetil S.
Jun 1 at 11:53
$begingroup$
Whats the maximum output? If every roll results in the maximum side? I think it's 7958661109946400884391936 = ((((6^2)^2)^2)^2)^2 = 6^(2^5) = 6^32
$endgroup$
– Kjetil S.
Jun 1 at 11:53
6
6
$begingroup$
@KjetilS. Indeed, and the likelihood of that output should be $frac16*prod_i=0^5(6^2^i)=frac16times 6times 6^2times 6^4times 6^8times 6^16times 6^32=frac12155416739906037495048372267884096782336$
$endgroup$
– Jonathan Allan
Jun 1 at 12:50
$begingroup$
@KjetilS. Indeed, and the likelihood of that output should be $frac16*prod_i=0^5(6^2^i)=frac16times 6times 6^2times 6^4times 6^8times 6^16times 6^32=frac12155416739906037495048372267884096782336$
$endgroup$
– Jonathan Allan
Jun 1 at 12:50
3
3
$begingroup$
Is this supposed to be random? The question doesn't mention anything about randomness at all?
$endgroup$
– Sriotchilism O'Zaic
Jun 1 at 16:35
$begingroup$
Is this supposed to be random? The question doesn't mention anything about randomness at all?
$endgroup$
– Sriotchilism O'Zaic
Jun 1 at 16:35
10
10
$begingroup$
@SriotchilismO'Zaic Dice-rolling implies randomness.
$endgroup$
– mbomb007
Jun 1 at 21:34
$begingroup$
@SriotchilismO'Zaic Dice-rolling implies randomness.
$endgroup$
– mbomb007
Jun 1 at 21:34
6
6
$begingroup$
@SriotchilismO'Zaic xkcd.com/221
$endgroup$
– Neyt
Jun 3 at 9:52
$begingroup$
@SriotchilismO'Zaic xkcd.com/221
$endgroup$
– Neyt
Jun 3 at 9:52
|
show 7 more comments
20 Answers
20
active
oldest
votes
$begingroup$
Python 2, 76 69 bytes
from random import*
R=randint
v=6;exec"p=R(1,v);v*=p;"*R(1,6)
print p
Try it online!
$endgroup$
add a comment
|
$begingroup$
Perl 6, 43 37 bytes
-6 bytes thanks to nwellnhof
(6,roll 1..[*] @_:...*)[1+6.rand]
Try it online!
Anonymous code block that returns the doomsday dice result.
Explanation:
# Anonymous code block
( )[1+6.rand] # Take a random number from
...* # The infinite list of
6,roll 1..[*] @_: # Cascading dice values
6, # Starting from 6
roll : # And choosing a random value from
1.. # One to
[*] @_ # The product of every value so far
$endgroup$
add a comment
|
$begingroup$
Wolfram Language (Mathematica), 43 bytes
(r=RandomInteger)@1,Nest[r@1,##&,6,r@5]
Try it online!
$endgroup$
add a comment
|
$begingroup$
J, 21 bytes
1+[:?(*1+?)^:(?`])@6x
Try it online!
+6 bytes thanks to a logic problem spotted by FrownyFrog
NOTE: J has no niladic verbs. However, this verb will work the same no matter what argument you give it. In the TIO example, I'm calling it with 0
, but I could have used 99
or ''
just as well.
how
1+
add one to...[:?
a single roll of an n-sided die (sides reading0
ton-1
), where the numbern
is determined by...(*1+?)
take the current argumenty
and roll?
to produce a random number between0
andy-1
.1+
makes that1
toy
, inclusive. Finally the*
creates a J hook, which will multiply that byy
again.^:
do the above this many times...(?`])
?
roll the initial argument, which is6
, to determine how many times to repeat. If we roll0
(corresponding to a1
on the Prime Bubble), the argument will pass through unchanged. The]
indicates that6
, unchanged, will be the starting value of repeated(*1+?)
verb that determines the die value for the final roll.@6x
attaches the constant verb6
, so that we can call it with anything, and thex
forces J to use extended integer computation which we need for the possibly huge numbers.
$endgroup$
$begingroup$
in this case 0 executes the previous verb once, 1 twice, etc
why is that?
$endgroup$
– FrownyFrog
Jun 1 at 23:15
$begingroup$
because i made a mistake :(. will fix soon.
$endgroup$
– Jonah
Jun 1 at 23:42
$begingroup$
Fixed now. Thanks.
$endgroup$
– Jonah
Jun 1 at 23:55
add a comment
|
$begingroup$
K (oK), 32 bytes
Solution:
*|x,1+1?x:(*).x/[*a;6,a:1+1?6]
Try it online!
Start with 6 and "1 choose 6", iterate over "1 choose 6" times:
*|x,1+1?x:(*).x/[*a;6,a:1+1?6] / the solution
/[n; c ] / iterate over lambda n times with starting condition c
1?6 / 1 choose 6, between 0..5 (returns a list of 1 item)
1+ / add 1 (so between 1..6)
a: / store as 'a'
6, / prepend 6, the number of sides of the first dice
*a / we are iterating between 0 and 5 times, take first (*)
(*).x / multi-argument apply (.) multiply (*) to x, e.g. 6*2 => 12
x: / save that as 'x'
1? / 1 choose x, between 0..x-1
1+ / add 1 (so between 1..x)
x, / prepend x
*| / reverse-first aka 'last'
You can see the iterations by switching the over for a scan, e.g.
(6 3 / 1 choose 6 => 3, so perform 3 iterations
18 15 / 1 choose (6*3=18) => 15
270 31 / 1 choose (18*15=270) => 31
8370 5280) / 1 choose (270*31=8730) => 5280
$endgroup$
1
$begingroup$
(*).x
->*/x
and/[*a;6,a:1+1?6]
->a
/6,a:*1+1?6
$endgroup$
– ngn
Jun 7 at 23:16
add a comment
|
$begingroup$
Jelly, 9 bytes
6X×$5СXX
A niladic Link which yields a positive integer.
Try it online!
Saving a byte over the more obvious 6X×$6X’¤¡X
How?
6X×$5СXX - Link: no arguments
6 - initialise left argument to 6
5С - repeat five times, collecting up as we go: -> a list of 6 possible dice sizes
$ - last two links as a monad = f(v): e.g [6,18,288,4032,1382976,216315425088]
X - random number in [1,v] or [6,6,6,6,6,6]
× - multiply (by v) or [6,36,1296,1679616,2821109907456,7958661109946400884391936]
X - random choice (since the input is now a list) -> faces (on final die)
X - random number in [1,faces]
$endgroup$
$begingroup$
Nice. I was trying to think of a way from moving beyond my ‘obvious’ answer, but didn’t think of generating all the dice and then picking one at random.
$endgroup$
– Nick Kennedy
Jun 1 at 13:50
$begingroup$
Heh, I missed that you'd posted almost that exact answer!
$endgroup$
– Jonathan Allan
Jun 1 at 14:01
add a comment
|
$begingroup$
05AB1E, 10 bytes
X6DLΩF*DLΩ
The random choice builtin for large lists is pretty slow, so may result in a timeout if the Prime Bubble roll is for example a 6.
Try it online or try it online with added prints to see the rolls. (TIO uses the legacy version of 05AB1E, since it's slightly faster.)
Explanation:
X # Push a 1 to the stack
6 # Push a 6 to the stack
D # Push another 6 to the stack
L # Pop the top 6, and push a list [1,2,3,4,5,6] to the stack
Ω # Pop and push a random item from this list (this is out Prime Bubble roll)
F # Loop that many times:
* # Multiply the top two values on the stack
# (which is why we had the initial 1 and duplicated 6 before the loop)
D # Duplicate this result
LΩ # Pop and push a random value from its ranged list again
# (after the loop, output the top of the stack implicitly)
$endgroup$
add a comment
|
$begingroup$
Jelly, 9 bytes
6×X$X’$¡X
Try it online!
Jonathan Allan's answer claims that it's
Saving a byte over the more obvious
6X×$6X’¤¡X
. In fact, we don't need to make such a big modification. Therefore, this is an alternative approach to Jonathan Allan's answer, and, also, a resting place for my initial invalid 6-byter. :(
$endgroup$
add a comment
|
$begingroup$
Perl 5, 54 bytes
$a=6;map$a*=$r=1+int rand$a,0..rand 6;say 1+int rand$r
Try it online!
$endgroup$
$begingroup$
That's one rand too many, you should just say$r
$endgroup$
– Grimy
Jun 1 at 19:25
add a comment
|
$begingroup$
Charcoal, 16 bytes
⊞υ⁶F⊕‽⁶⊞υ⊕‽ΠυI⊟υ
Try it online! Link is to verbose version of code. Explanation:
⊞υ⁶
Push 6 to the predefined list.
F⊕‽⁶
Repeat a random number of times from 1 to 6...
⊞υ⊕‽Πυ
... push a random number between 1 and the product of the list to the list.
I⊟υ
Output the last number pushed to the list.
Alternative approach, also 16 bytes
≔⁶θF‽⁶≧×⊕‽θθI⊕‽θ
Try it online! Link is to verbose version of code. Explanation:
≔⁶θ
Set the number of sides to 6.
F‽⁶
Repeat a random number between 0 and 5 times...
≧×⊕‽θθ
... multiply the number of sides by a random number from 1 to the number of sides.
I⊕‽θ
Print a random number from 1 to the number of sides.
$endgroup$
add a comment
|
$begingroup$
Python 3, 76 bytes
from random import*
r=randint
a=6
for i in" "*r(0,5):a*=r(1,a)
print(r(1,a))
Try it online!
-2 bytes thanks to TFeld
$endgroup$
1
$begingroup$
-2 bytes by changing the import
$endgroup$
– TFeld
Jun 1 at 7:34
$begingroup$
@TFeld thanks [filler]
$endgroup$
– HyperNeutrino
Jun 1 at 19:23
add a comment
|
$begingroup$
R, 43 bytes
s=sample
for(i in 1:s(k<-6))T=s(k<-k*T,1)
T
Try it online!
k
keeps track of the current number of faces on the die. Uses the fact that T
is initialized as 1
.
I tried a few other things, but couldn't beat this simple, straightforward approach.
$endgroup$
add a comment
|
$begingroup$
Jelly, 10 bytes
6×X$6X’¤¡X
Try it online!
Explanation
¤ | Following as a nilad:
6X | - A random number between 1 and 6
’ | - Decrease by 1 (call this N)
6 | Now, start with 6
$ ¡ | Repeat the following N times, as a monad
× | - Multiply by:
X | - A random number between 1 and the current total
X | Finally, generate a random number between 1 and the output of the above loop
$endgroup$
add a comment
|
$begingroup$
Ruby, 41 bytes
r=6;rand(2..7).timesr*=$s=rand 1..r;p$s
Try it online!
Explanation
r=6 # Set dice number to 6
rand(2..7).times # Repeat X times, where X=dice roll+1
r*=$s=rand 1..r # Multiply dice number by a dice roll
# Save the most recent dice roll
p$s # Print last dice roll (this is why
# we did the last step one extra time)
$endgroup$
add a comment
|
$begingroup$
Java 10, 214 93 86 bytes
v->int r=6,n=0;for(var d=Math.random()*6;d-->0;n*=Math.random(),r*=++n)n=r;return n;
Try it online or try it online with additional print-lines to see the steps.
-128 bytes by using int
instead of java.math.BigInteger
. $6^32$, the largest possible result, doesn't fit inside an int
nor long
, which is why BigInteger
was used initially. OP allowed to use int
and assuming it's infinitely large, so that saved more than 125 bytes here. :) (Here the previous 214 bytes version using BigIntegers
.)
Explanation:
v-> // Method with empty unused parameter & integer return-type
int r=6, // The range in which to roll, starting at 6
n=0; // The roll itself (which must be initialized, therefor is 0)
for(var d=Math.random()*6;// Roll the Prime Bubble Dice
d-->0 // Loop that many times:
; // After every iteration:
n*=Math.random(), // Roll a random dice in the range [0, n)
r*=++n) // Increase `n` by 1 first with `++n`, so the range is [1,n]
// And then multiply `r` by `n` for the new range
n=r; // Set `n` to `r`
return n; // After the loop, return `n` as result
$endgroup$
$begingroup$
Post the solution that doesn't do BigInteger as your competing solution.
$endgroup$
– Stackstuck
Jun 3 at 16:16
$begingroup$
@Stackstuck Will await what OP has to say to your comment. Was about to make one myself to confirm. I personally use BigIntegers/BigDecimals for challenges where it's necessary, unless OP explicitly states you can use your native max integer size (which is usually $2^32$, soint
for both Java and .NET C#).
$endgroup$
– Kevin Cruijssen
Jun 3 at 16:26
$begingroup$
I'll see if I can find anything on meta about this.
$endgroup$
– Stackstuck
Jun 3 at 16:29
1
$begingroup$
OP says don't worry about integer size limits. Use theint
type.
$endgroup$
– Stackstuck
Jun 8 at 1:26
1
$begingroup$
@Stackstuck Done, and golfed 7 bytes in the process. :)
$endgroup$
– Kevin Cruijssen
Jun 8 at 12:00
add a comment
|
$begingroup$
PHP, 59 bytes
$r=$q=rand(1,$s=6);while($l++<$q)$r=rand(1,$s*=$r);print$r;
expanded:
$r=$q=rand(1,$s=6);
while($l++<$q)$
r=rand(1,$s*=$r);
print$r;
Not sure if I'm supposed to include the open tag.
On my machine, it crashes if $s*$r
is too large, so it doesn't print on $q>=5
sometimes... because the numbers get so big. Not sure of a fix.
$endgroup$
add a comment
|
$begingroup$
Pyth, 14 bytes
uhO=*|Z6GO6hO6
Try it online!
uhO=*|Z6GO6hO6
O6 Random number in range [0-6)
u Perform the following the above number of times...
hO6 ... with starting value G a random number in range [1-6]:
* G Multiply G with...
|Z6 The value of Z, or 6 if it's the first time through (Z is 0 at program start)
= Z Assign the above back into Z
O Random number in range [0-Z)
h Increment
Implicit print result of final iteration
$endgroup$
add a comment
|
$begingroup$
C# (.NET Core), 136 bytes
class Astatic void Main()var r=new System.Random();int i=r.Next(6),j=6;while(i-->0)j*=1+r.Next(j);System.Console.Write(r.Next(j)+1);
Try it online!
I'm pretty sure this works, given the assumption of infinite integer length that we're fond of here. If I have to actually handle the overflow, I'd need to bust out an entirely different class.
$endgroup$
$begingroup$
It can now give aSystem.ArgumentOutOfRangeException: 'maxValue' must be greater than zero
error. The max result can be $6^32$, which is larger than the max size of bothint
andlong
, so you'll have to useBigIntegers
instead.
$endgroup$
– Kevin Cruijssen
Jun 3 at 9:34
$begingroup$
@KevinCruijssen yes, that's the whole point of my comment.
$endgroup$
– Stackstuck
Jun 3 at 16:12
add a comment
|
$begingroup$
Julia 1.0, 60 bytes
g(b=big(6),r=rand)=(for i in 1:r(0:5) b=b*r(1:b) end;r(1:b))
b=big(6)
makes it work with arbitrary sized integers
Try it online!
$endgroup$
add a comment
|
$begingroup$
Gaia, 13 bytes
:(ṛ×
6₅ṛ(↑ₓ(ṛ
Try it online!
$endgroup$
add a comment
|
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "200"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f186294%2fimplement-homestucks-catenative-doomsday-dice-cascader%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
20 Answers
20
active
oldest
votes
20 Answers
20
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Python 2, 76 69 bytes
from random import*
R=randint
v=6;exec"p=R(1,v);v*=p;"*R(1,6)
print p
Try it online!
$endgroup$
add a comment
|
$begingroup$
Python 2, 76 69 bytes
from random import*
R=randint
v=6;exec"p=R(1,v);v*=p;"*R(1,6)
print p
Try it online!
$endgroup$
add a comment
|
$begingroup$
Python 2, 76 69 bytes
from random import*
R=randint
v=6;exec"p=R(1,v);v*=p;"*R(1,6)
print p
Try it online!
$endgroup$
Python 2, 76 69 bytes
from random import*
R=randint
v=6;exec"p=R(1,v);v*=p;"*R(1,6)
print p
Try it online!
edited Jun 1 at 3:28
answered Jun 1 at 3:12
Chas BrownChas Brown
6,8641 gold badge6 silver badges27 bronze badges
6,8641 gold badge6 silver badges27 bronze badges
add a comment
|
add a comment
|
$begingroup$
Perl 6, 43 37 bytes
-6 bytes thanks to nwellnhof
(6,roll 1..[*] @_:...*)[1+6.rand]
Try it online!
Anonymous code block that returns the doomsday dice result.
Explanation:
# Anonymous code block
( )[1+6.rand] # Take a random number from
...* # The infinite list of
6,roll 1..[*] @_: # Cascading dice values
6, # Starting from 6
roll : # And choosing a random value from
1.. # One to
[*] @_ # The product of every value so far
$endgroup$
add a comment
|
$begingroup$
Perl 6, 43 37 bytes
-6 bytes thanks to nwellnhof
(6,roll 1..[*] @_:...*)[1+6.rand]
Try it online!
Anonymous code block that returns the doomsday dice result.
Explanation:
# Anonymous code block
( )[1+6.rand] # Take a random number from
...* # The infinite list of
6,roll 1..[*] @_: # Cascading dice values
6, # Starting from 6
roll : # And choosing a random value from
1.. # One to
[*] @_ # The product of every value so far
$endgroup$
add a comment
|
$begingroup$
Perl 6, 43 37 bytes
-6 bytes thanks to nwellnhof
(6,roll 1..[*] @_:...*)[1+6.rand]
Try it online!
Anonymous code block that returns the doomsday dice result.
Explanation:
# Anonymous code block
( )[1+6.rand] # Take a random number from
...* # The infinite list of
6,roll 1..[*] @_: # Cascading dice values
6, # Starting from 6
roll : # And choosing a random value from
1.. # One to
[*] @_ # The product of every value so far
$endgroup$
Perl 6, 43 37 bytes
-6 bytes thanks to nwellnhof
(6,roll 1..[*] @_:...*)[1+6.rand]
Try it online!
Anonymous code block that returns the doomsday dice result.
Explanation:
# Anonymous code block
( )[1+6.rand] # Take a random number from
...* # The infinite list of
6,roll 1..[*] @_: # Cascading dice values
6, # Starting from 6
roll : # And choosing a random value from
1.. # One to
[*] @_ # The product of every value so far
edited Jun 1 at 9:42
answered Jun 1 at 6:23
Jo KingJo King
33.1k4 gold badges75 silver badges142 bronze badges
33.1k4 gold badges75 silver badges142 bronze badges
add a comment
|
add a comment
|
$begingroup$
Wolfram Language (Mathematica), 43 bytes
(r=RandomInteger)@1,Nest[r@1,##&,6,r@5]
Try it online!
$endgroup$
add a comment
|
$begingroup$
Wolfram Language (Mathematica), 43 bytes
(r=RandomInteger)@1,Nest[r@1,##&,6,r@5]
Try it online!
$endgroup$
add a comment
|
$begingroup$
Wolfram Language (Mathematica), 43 bytes
(r=RandomInteger)@1,Nest[r@1,##&,6,r@5]
Try it online!
$endgroup$
Wolfram Language (Mathematica), 43 bytes
(r=RandomInteger)@1,Nest[r@1,##&,6,r@5]
Try it online!
edited Jun 1 at 11:47
answered Jun 1 at 11:40
attinatattinat
3,1063 silver badges11 bronze badges
3,1063 silver badges11 bronze badges
add a comment
|
add a comment
|
$begingroup$
J, 21 bytes
1+[:?(*1+?)^:(?`])@6x
Try it online!
+6 bytes thanks to a logic problem spotted by FrownyFrog
NOTE: J has no niladic verbs. However, this verb will work the same no matter what argument you give it. In the TIO example, I'm calling it with 0
, but I could have used 99
or ''
just as well.
how
1+
add one to...[:?
a single roll of an n-sided die (sides reading0
ton-1
), where the numbern
is determined by...(*1+?)
take the current argumenty
and roll?
to produce a random number between0
andy-1
.1+
makes that1
toy
, inclusive. Finally the*
creates a J hook, which will multiply that byy
again.^:
do the above this many times...(?`])
?
roll the initial argument, which is6
, to determine how many times to repeat. If we roll0
(corresponding to a1
on the Prime Bubble), the argument will pass through unchanged. The]
indicates that6
, unchanged, will be the starting value of repeated(*1+?)
verb that determines the die value for the final roll.@6x
attaches the constant verb6
, so that we can call it with anything, and thex
forces J to use extended integer computation which we need for the possibly huge numbers.
$endgroup$
$begingroup$
in this case 0 executes the previous verb once, 1 twice, etc
why is that?
$endgroup$
– FrownyFrog
Jun 1 at 23:15
$begingroup$
because i made a mistake :(. will fix soon.
$endgroup$
– Jonah
Jun 1 at 23:42
$begingroup$
Fixed now. Thanks.
$endgroup$
– Jonah
Jun 1 at 23:55
add a comment
|
$begingroup$
J, 21 bytes
1+[:?(*1+?)^:(?`])@6x
Try it online!
+6 bytes thanks to a logic problem spotted by FrownyFrog
NOTE: J has no niladic verbs. However, this verb will work the same no matter what argument you give it. In the TIO example, I'm calling it with 0
, but I could have used 99
or ''
just as well.
how
1+
add one to...[:?
a single roll of an n-sided die (sides reading0
ton-1
), where the numbern
is determined by...(*1+?)
take the current argumenty
and roll?
to produce a random number between0
andy-1
.1+
makes that1
toy
, inclusive. Finally the*
creates a J hook, which will multiply that byy
again.^:
do the above this many times...(?`])
?
roll the initial argument, which is6
, to determine how many times to repeat. If we roll0
(corresponding to a1
on the Prime Bubble), the argument will pass through unchanged. The]
indicates that6
, unchanged, will be the starting value of repeated(*1+?)
verb that determines the die value for the final roll.@6x
attaches the constant verb6
, so that we can call it with anything, and thex
forces J to use extended integer computation which we need for the possibly huge numbers.
$endgroup$
$begingroup$
in this case 0 executes the previous verb once, 1 twice, etc
why is that?
$endgroup$
– FrownyFrog
Jun 1 at 23:15
$begingroup$
because i made a mistake :(. will fix soon.
$endgroup$
– Jonah
Jun 1 at 23:42
$begingroup$
Fixed now. Thanks.
$endgroup$
– Jonah
Jun 1 at 23:55
add a comment
|
$begingroup$
J, 21 bytes
1+[:?(*1+?)^:(?`])@6x
Try it online!
+6 bytes thanks to a logic problem spotted by FrownyFrog
NOTE: J has no niladic verbs. However, this verb will work the same no matter what argument you give it. In the TIO example, I'm calling it with 0
, but I could have used 99
or ''
just as well.
how
1+
add one to...[:?
a single roll of an n-sided die (sides reading0
ton-1
), where the numbern
is determined by...(*1+?)
take the current argumenty
and roll?
to produce a random number between0
andy-1
.1+
makes that1
toy
, inclusive. Finally the*
creates a J hook, which will multiply that byy
again.^:
do the above this many times...(?`])
?
roll the initial argument, which is6
, to determine how many times to repeat. If we roll0
(corresponding to a1
on the Prime Bubble), the argument will pass through unchanged. The]
indicates that6
, unchanged, will be the starting value of repeated(*1+?)
verb that determines the die value for the final roll.@6x
attaches the constant verb6
, so that we can call it with anything, and thex
forces J to use extended integer computation which we need for the possibly huge numbers.
$endgroup$
J, 21 bytes
1+[:?(*1+?)^:(?`])@6x
Try it online!
+6 bytes thanks to a logic problem spotted by FrownyFrog
NOTE: J has no niladic verbs. However, this verb will work the same no matter what argument you give it. In the TIO example, I'm calling it with 0
, but I could have used 99
or ''
just as well.
how
1+
add one to...[:?
a single roll of an n-sided die (sides reading0
ton-1
), where the numbern
is determined by...(*1+?)
take the current argumenty
and roll?
to produce a random number between0
andy-1
.1+
makes that1
toy
, inclusive. Finally the*
creates a J hook, which will multiply that byy
again.^:
do the above this many times...(?`])
?
roll the initial argument, which is6
, to determine how many times to repeat. If we roll0
(corresponding to a1
on the Prime Bubble), the argument will pass through unchanged. The]
indicates that6
, unchanged, will be the starting value of repeated(*1+?)
verb that determines the die value for the final roll.@6x
attaches the constant verb6
, so that we can call it with anything, and thex
forces J to use extended integer computation which we need for the possibly huge numbers.
edited Jun 2 at 0:25
answered Jun 1 at 4:15
JonahJonah
5,4372 gold badges13 silver badges25 bronze badges
5,4372 gold badges13 silver badges25 bronze badges
$begingroup$
in this case 0 executes the previous verb once, 1 twice, etc
why is that?
$endgroup$
– FrownyFrog
Jun 1 at 23:15
$begingroup$
because i made a mistake :(. will fix soon.
$endgroup$
– Jonah
Jun 1 at 23:42
$begingroup$
Fixed now. Thanks.
$endgroup$
– Jonah
Jun 1 at 23:55
add a comment
|
$begingroup$
in this case 0 executes the previous verb once, 1 twice, etc
why is that?
$endgroup$
– FrownyFrog
Jun 1 at 23:15
$begingroup$
because i made a mistake :(. will fix soon.
$endgroup$
– Jonah
Jun 1 at 23:42
$begingroup$
Fixed now. Thanks.
$endgroup$
– Jonah
Jun 1 at 23:55
$begingroup$
in this case 0 executes the previous verb once, 1 twice, etc
why is that?$endgroup$
– FrownyFrog
Jun 1 at 23:15
$begingroup$
in this case 0 executes the previous verb once, 1 twice, etc
why is that?$endgroup$
– FrownyFrog
Jun 1 at 23:15
$begingroup$
because i made a mistake :(. will fix soon.
$endgroup$
– Jonah
Jun 1 at 23:42
$begingroup$
because i made a mistake :(. will fix soon.
$endgroup$
– Jonah
Jun 1 at 23:42
$begingroup$
Fixed now. Thanks.
$endgroup$
– Jonah
Jun 1 at 23:55
$begingroup$
Fixed now. Thanks.
$endgroup$
– Jonah
Jun 1 at 23:55
add a comment
|
$begingroup$
K (oK), 32 bytes
Solution:
*|x,1+1?x:(*).x/[*a;6,a:1+1?6]
Try it online!
Start with 6 and "1 choose 6", iterate over "1 choose 6" times:
*|x,1+1?x:(*).x/[*a;6,a:1+1?6] / the solution
/[n; c ] / iterate over lambda n times with starting condition c
1?6 / 1 choose 6, between 0..5 (returns a list of 1 item)
1+ / add 1 (so between 1..6)
a: / store as 'a'
6, / prepend 6, the number of sides of the first dice
*a / we are iterating between 0 and 5 times, take first (*)
(*).x / multi-argument apply (.) multiply (*) to x, e.g. 6*2 => 12
x: / save that as 'x'
1? / 1 choose x, between 0..x-1
1+ / add 1 (so between 1..x)
x, / prepend x
*| / reverse-first aka 'last'
You can see the iterations by switching the over for a scan, e.g.
(6 3 / 1 choose 6 => 3, so perform 3 iterations
18 15 / 1 choose (6*3=18) => 15
270 31 / 1 choose (18*15=270) => 31
8370 5280) / 1 choose (270*31=8730) => 5280
$endgroup$
1
$begingroup$
(*).x
->*/x
and/[*a;6,a:1+1?6]
->a
/6,a:*1+1?6
$endgroup$
– ngn
Jun 7 at 23:16
add a comment
|
$begingroup$
K (oK), 32 bytes
Solution:
*|x,1+1?x:(*).x/[*a;6,a:1+1?6]
Try it online!
Start with 6 and "1 choose 6", iterate over "1 choose 6" times:
*|x,1+1?x:(*).x/[*a;6,a:1+1?6] / the solution
/[n; c ] / iterate over lambda n times with starting condition c
1?6 / 1 choose 6, between 0..5 (returns a list of 1 item)
1+ / add 1 (so between 1..6)
a: / store as 'a'
6, / prepend 6, the number of sides of the first dice
*a / we are iterating between 0 and 5 times, take first (*)
(*).x / multi-argument apply (.) multiply (*) to x, e.g. 6*2 => 12
x: / save that as 'x'
1? / 1 choose x, between 0..x-1
1+ / add 1 (so between 1..x)
x, / prepend x
*| / reverse-first aka 'last'
You can see the iterations by switching the over for a scan, e.g.
(6 3 / 1 choose 6 => 3, so perform 3 iterations
18 15 / 1 choose (6*3=18) => 15
270 31 / 1 choose (18*15=270) => 31
8370 5280) / 1 choose (270*31=8730) => 5280
$endgroup$
1
$begingroup$
(*).x
->*/x
and/[*a;6,a:1+1?6]
->a
/6,a:*1+1?6
$endgroup$
– ngn
Jun 7 at 23:16
add a comment
|
$begingroup$
K (oK), 32 bytes
Solution:
*|x,1+1?x:(*).x/[*a;6,a:1+1?6]
Try it online!
Start with 6 and "1 choose 6", iterate over "1 choose 6" times:
*|x,1+1?x:(*).x/[*a;6,a:1+1?6] / the solution
/[n; c ] / iterate over lambda n times with starting condition c
1?6 / 1 choose 6, between 0..5 (returns a list of 1 item)
1+ / add 1 (so between 1..6)
a: / store as 'a'
6, / prepend 6, the number of sides of the first dice
*a / we are iterating between 0 and 5 times, take first (*)
(*).x / multi-argument apply (.) multiply (*) to x, e.g. 6*2 => 12
x: / save that as 'x'
1? / 1 choose x, between 0..x-1
1+ / add 1 (so between 1..x)
x, / prepend x
*| / reverse-first aka 'last'
You can see the iterations by switching the over for a scan, e.g.
(6 3 / 1 choose 6 => 3, so perform 3 iterations
18 15 / 1 choose (6*3=18) => 15
270 31 / 1 choose (18*15=270) => 31
8370 5280) / 1 choose (270*31=8730) => 5280
$endgroup$
K (oK), 32 bytes
Solution:
*|x,1+1?x:(*).x/[*a;6,a:1+1?6]
Try it online!
Start with 6 and "1 choose 6", iterate over "1 choose 6" times:
*|x,1+1?x:(*).x/[*a;6,a:1+1?6] / the solution
/[n; c ] / iterate over lambda n times with starting condition c
1?6 / 1 choose 6, between 0..5 (returns a list of 1 item)
1+ / add 1 (so between 1..6)
a: / store as 'a'
6, / prepend 6, the number of sides of the first dice
*a / we are iterating between 0 and 5 times, take first (*)
(*).x / multi-argument apply (.) multiply (*) to x, e.g. 6*2 => 12
x: / save that as 'x'
1? / 1 choose x, between 0..x-1
1+ / add 1 (so between 1..x)
x, / prepend x
*| / reverse-first aka 'last'
You can see the iterations by switching the over for a scan, e.g.
(6 3 / 1 choose 6 => 3, so perform 3 iterations
18 15 / 1 choose (6*3=18) => 15
270 31 / 1 choose (18*15=270) => 31
8370 5280) / 1 choose (270*31=8730) => 5280
edited Jun 1 at 7:24
answered Jun 1 at 7:17
streetsterstreetster
3,2756 silver badges16 bronze badges
3,2756 silver badges16 bronze badges
1
$begingroup$
(*).x
->*/x
and/[*a;6,a:1+1?6]
->a
/6,a:*1+1?6
$endgroup$
– ngn
Jun 7 at 23:16
add a comment
|
1
$begingroup$
(*).x
->*/x
and/[*a;6,a:1+1?6]
->a
/6,a:*1+1?6
$endgroup$
– ngn
Jun 7 at 23:16
1
1
$begingroup$
(*).x
-> */x
and
/[*a;6,a:1+1?6]
-> a
/6,a:*1+1?6
$endgroup$
– ngn
Jun 7 at 23:16
$begingroup$
(*).x
-> */x
and
/[*a;6,a:1+1?6]
-> a
/6,a:*1+1?6
$endgroup$
– ngn
Jun 7 at 23:16
add a comment
|
$begingroup$
Jelly, 9 bytes
6X×$5СXX
A niladic Link which yields a positive integer.
Try it online!
Saving a byte over the more obvious 6X×$6X’¤¡X
How?
6X×$5СXX - Link: no arguments
6 - initialise left argument to 6
5С - repeat five times, collecting up as we go: -> a list of 6 possible dice sizes
$ - last two links as a monad = f(v): e.g [6,18,288,4032,1382976,216315425088]
X - random number in [1,v] or [6,6,6,6,6,6]
× - multiply (by v) or [6,36,1296,1679616,2821109907456,7958661109946400884391936]
X - random choice (since the input is now a list) -> faces (on final die)
X - random number in [1,faces]
$endgroup$
$begingroup$
Nice. I was trying to think of a way from moving beyond my ‘obvious’ answer, but didn’t think of generating all the dice and then picking one at random.
$endgroup$
– Nick Kennedy
Jun 1 at 13:50
$begingroup$
Heh, I missed that you'd posted almost that exact answer!
$endgroup$
– Jonathan Allan
Jun 1 at 14:01
add a comment
|
$begingroup$
Jelly, 9 bytes
6X×$5СXX
A niladic Link which yields a positive integer.
Try it online!
Saving a byte over the more obvious 6X×$6X’¤¡X
How?
6X×$5СXX - Link: no arguments
6 - initialise left argument to 6
5С - repeat five times, collecting up as we go: -> a list of 6 possible dice sizes
$ - last two links as a monad = f(v): e.g [6,18,288,4032,1382976,216315425088]
X - random number in [1,v] or [6,6,6,6,6,6]
× - multiply (by v) or [6,36,1296,1679616,2821109907456,7958661109946400884391936]
X - random choice (since the input is now a list) -> faces (on final die)
X - random number in [1,faces]
$endgroup$
$begingroup$
Nice. I was trying to think of a way from moving beyond my ‘obvious’ answer, but didn’t think of generating all the dice and then picking one at random.
$endgroup$
– Nick Kennedy
Jun 1 at 13:50
$begingroup$
Heh, I missed that you'd posted almost that exact answer!
$endgroup$
– Jonathan Allan
Jun 1 at 14:01
add a comment
|
$begingroup$
Jelly, 9 bytes
6X×$5СXX
A niladic Link which yields a positive integer.
Try it online!
Saving a byte over the more obvious 6X×$6X’¤¡X
How?
6X×$5СXX - Link: no arguments
6 - initialise left argument to 6
5С - repeat five times, collecting up as we go: -> a list of 6 possible dice sizes
$ - last two links as a monad = f(v): e.g [6,18,288,4032,1382976,216315425088]
X - random number in [1,v] or [6,6,6,6,6,6]
× - multiply (by v) or [6,36,1296,1679616,2821109907456,7958661109946400884391936]
X - random choice (since the input is now a list) -> faces (on final die)
X - random number in [1,faces]
$endgroup$
Jelly, 9 bytes
6X×$5СXX
A niladic Link which yields a positive integer.
Try it online!
Saving a byte over the more obvious 6X×$6X’¤¡X
How?
6X×$5СXX - Link: no arguments
6 - initialise left argument to 6
5С - repeat five times, collecting up as we go: -> a list of 6 possible dice sizes
$ - last two links as a monad = f(v): e.g [6,18,288,4032,1382976,216315425088]
X - random number in [1,v] or [6,6,6,6,6,6]
× - multiply (by v) or [6,36,1296,1679616,2821109907456,7958661109946400884391936]
X - random choice (since the input is now a list) -> faces (on final die)
X - random number in [1,faces]
edited Jun 1 at 13:58
answered Jun 1 at 13:47
Jonathan AllanJonathan Allan
61.9k5 gold badges44 silver badges188 bronze badges
61.9k5 gold badges44 silver badges188 bronze badges
$begingroup$
Nice. I was trying to think of a way from moving beyond my ‘obvious’ answer, but didn’t think of generating all the dice and then picking one at random.
$endgroup$
– Nick Kennedy
Jun 1 at 13:50
$begingroup$
Heh, I missed that you'd posted almost that exact answer!
$endgroup$
– Jonathan Allan
Jun 1 at 14:01
add a comment
|
$begingroup$
Nice. I was trying to think of a way from moving beyond my ‘obvious’ answer, but didn’t think of generating all the dice and then picking one at random.
$endgroup$
– Nick Kennedy
Jun 1 at 13:50
$begingroup$
Heh, I missed that you'd posted almost that exact answer!
$endgroup$
– Jonathan Allan
Jun 1 at 14:01
$begingroup$
Nice. I was trying to think of a way from moving beyond my ‘obvious’ answer, but didn’t think of generating all the dice and then picking one at random.
$endgroup$
– Nick Kennedy
Jun 1 at 13:50
$begingroup$
Nice. I was trying to think of a way from moving beyond my ‘obvious’ answer, but didn’t think of generating all the dice and then picking one at random.
$endgroup$
– Nick Kennedy
Jun 1 at 13:50
$begingroup$
Heh, I missed that you'd posted almost that exact answer!
$endgroup$
– Jonathan Allan
Jun 1 at 14:01
$begingroup$
Heh, I missed that you'd posted almost that exact answer!
$endgroup$
– Jonathan Allan
Jun 1 at 14:01
add a comment
|
$begingroup$
05AB1E, 10 bytes
X6DLΩF*DLΩ
The random choice builtin for large lists is pretty slow, so may result in a timeout if the Prime Bubble roll is for example a 6.
Try it online or try it online with added prints to see the rolls. (TIO uses the legacy version of 05AB1E, since it's slightly faster.)
Explanation:
X # Push a 1 to the stack
6 # Push a 6 to the stack
D # Push another 6 to the stack
L # Pop the top 6, and push a list [1,2,3,4,5,6] to the stack
Ω # Pop and push a random item from this list (this is out Prime Bubble roll)
F # Loop that many times:
* # Multiply the top two values on the stack
# (which is why we had the initial 1 and duplicated 6 before the loop)
D # Duplicate this result
LΩ # Pop and push a random value from its ranged list again
# (after the loop, output the top of the stack implicitly)
$endgroup$
add a comment
|
$begingroup$
05AB1E, 10 bytes
X6DLΩF*DLΩ
The random choice builtin for large lists is pretty slow, so may result in a timeout if the Prime Bubble roll is for example a 6.
Try it online or try it online with added prints to see the rolls. (TIO uses the legacy version of 05AB1E, since it's slightly faster.)
Explanation:
X # Push a 1 to the stack
6 # Push a 6 to the stack
D # Push another 6 to the stack
L # Pop the top 6, and push a list [1,2,3,4,5,6] to the stack
Ω # Pop and push a random item from this list (this is out Prime Bubble roll)
F # Loop that many times:
* # Multiply the top two values on the stack
# (which is why we had the initial 1 and duplicated 6 before the loop)
D # Duplicate this result
LΩ # Pop and push a random value from its ranged list again
# (after the loop, output the top of the stack implicitly)
$endgroup$
add a comment
|
$begingroup$
05AB1E, 10 bytes
X6DLΩF*DLΩ
The random choice builtin for large lists is pretty slow, so may result in a timeout if the Prime Bubble roll is for example a 6.
Try it online or try it online with added prints to see the rolls. (TIO uses the legacy version of 05AB1E, since it's slightly faster.)
Explanation:
X # Push a 1 to the stack
6 # Push a 6 to the stack
D # Push another 6 to the stack
L # Pop the top 6, and push a list [1,2,3,4,5,6] to the stack
Ω # Pop and push a random item from this list (this is out Prime Bubble roll)
F # Loop that many times:
* # Multiply the top two values on the stack
# (which is why we had the initial 1 and duplicated 6 before the loop)
D # Duplicate this result
LΩ # Pop and push a random value from its ranged list again
# (after the loop, output the top of the stack implicitly)
$endgroup$
05AB1E, 10 bytes
X6DLΩF*DLΩ
The random choice builtin for large lists is pretty slow, so may result in a timeout if the Prime Bubble roll is for example a 6.
Try it online or try it online with added prints to see the rolls. (TIO uses the legacy version of 05AB1E, since it's slightly faster.)
Explanation:
X # Push a 1 to the stack
6 # Push a 6 to the stack
D # Push another 6 to the stack
L # Pop the top 6, and push a list [1,2,3,4,5,6] to the stack
Ω # Pop and push a random item from this list (this is out Prime Bubble roll)
F # Loop that many times:
* # Multiply the top two values on the stack
# (which is why we had the initial 1 and duplicated 6 before the loop)
D # Duplicate this result
LΩ # Pop and push a random value from its ranged list again
# (after the loop, output the top of the stack implicitly)
answered Jun 1 at 13:50
Kevin CruijssenKevin Cruijssen
52.4k7 gold badges85 silver badges252 bronze badges
52.4k7 gold badges85 silver badges252 bronze badges
add a comment
|
add a comment
|
$begingroup$
Jelly, 9 bytes
6×X$X’$¡X
Try it online!
Jonathan Allan's answer claims that it's
Saving a byte over the more obvious
6X×$6X’¤¡X
. In fact, we don't need to make such a big modification. Therefore, this is an alternative approach to Jonathan Allan's answer, and, also, a resting place for my initial invalid 6-byter. :(
$endgroup$
add a comment
|
$begingroup$
Jelly, 9 bytes
6×X$X’$¡X
Try it online!
Jonathan Allan's answer claims that it's
Saving a byte over the more obvious
6X×$6X’¤¡X
. In fact, we don't need to make such a big modification. Therefore, this is an alternative approach to Jonathan Allan's answer, and, also, a resting place for my initial invalid 6-byter. :(
$endgroup$
add a comment
|
$begingroup$
Jelly, 9 bytes
6×X$X’$¡X
Try it online!
Jonathan Allan's answer claims that it's
Saving a byte over the more obvious
6X×$6X’¤¡X
. In fact, we don't need to make such a big modification. Therefore, this is an alternative approach to Jonathan Allan's answer, and, also, a resting place for my initial invalid 6-byter. :(
$endgroup$
Jelly, 9 bytes
6×X$X’$¡X
Try it online!
Jonathan Allan's answer claims that it's
Saving a byte over the more obvious
6X×$6X’¤¡X
. In fact, we don't need to make such a big modification. Therefore, this is an alternative approach to Jonathan Allan's answer, and, also, a resting place for my initial invalid 6-byter. :(
edited Jun 2 at 0:55
answered Jun 2 at 0:06
Erik the OutgolferErik the Outgolfer
36.6k4 gold badges31 silver badges113 bronze badges
36.6k4 gold badges31 silver badges113 bronze badges
add a comment
|
add a comment
|
$begingroup$
Perl 5, 54 bytes
$a=6;map$a*=$r=1+int rand$a,0..rand 6;say 1+int rand$r
Try it online!
$endgroup$
$begingroup$
That's one rand too many, you should just say$r
$endgroup$
– Grimy
Jun 1 at 19:25
add a comment
|
$begingroup$
Perl 5, 54 bytes
$a=6;map$a*=$r=1+int rand$a,0..rand 6;say 1+int rand$r
Try it online!
$endgroup$
$begingroup$
That's one rand too many, you should just say$r
$endgroup$
– Grimy
Jun 1 at 19:25
add a comment
|
$begingroup$
Perl 5, 54 bytes
$a=6;map$a*=$r=1+int rand$a,0..rand 6;say 1+int rand$r
Try it online!
$endgroup$
Perl 5, 54 bytes
$a=6;map$a*=$r=1+int rand$a,0..rand 6;say 1+int rand$r
Try it online!
answered Jun 1 at 4:01
XcaliXcali
6,9951 gold badge7 silver badges24 bronze badges
6,9951 gold badge7 silver badges24 bronze badges
$begingroup$
That's one rand too many, you should just say$r
$endgroup$
– Grimy
Jun 1 at 19:25
add a comment
|
$begingroup$
That's one rand too many, you should just say$r
$endgroup$
– Grimy
Jun 1 at 19:25
$begingroup$
That's one rand too many, you should just say$r
$endgroup$
– Grimy
Jun 1 at 19:25
$begingroup$
That's one rand too many, you should just say$r
$endgroup$
– Grimy
Jun 1 at 19:25
add a comment
|
$begingroup$
Charcoal, 16 bytes
⊞υ⁶F⊕‽⁶⊞υ⊕‽ΠυI⊟υ
Try it online! Link is to verbose version of code. Explanation:
⊞υ⁶
Push 6 to the predefined list.
F⊕‽⁶
Repeat a random number of times from 1 to 6...
⊞υ⊕‽Πυ
... push a random number between 1 and the product of the list to the list.
I⊟υ
Output the last number pushed to the list.
Alternative approach, also 16 bytes
≔⁶θF‽⁶≧×⊕‽θθI⊕‽θ
Try it online! Link is to verbose version of code. Explanation:
≔⁶θ
Set the number of sides to 6.
F‽⁶
Repeat a random number between 0 and 5 times...
≧×⊕‽θθ
... multiply the number of sides by a random number from 1 to the number of sides.
I⊕‽θ
Print a random number from 1 to the number of sides.
$endgroup$
add a comment
|
$begingroup$
Charcoal, 16 bytes
⊞υ⁶F⊕‽⁶⊞υ⊕‽ΠυI⊟υ
Try it online! Link is to verbose version of code. Explanation:
⊞υ⁶
Push 6 to the predefined list.
F⊕‽⁶
Repeat a random number of times from 1 to 6...
⊞υ⊕‽Πυ
... push a random number between 1 and the product of the list to the list.
I⊟υ
Output the last number pushed to the list.
Alternative approach, also 16 bytes
≔⁶θF‽⁶≧×⊕‽θθI⊕‽θ
Try it online! Link is to verbose version of code. Explanation:
≔⁶θ
Set the number of sides to 6.
F‽⁶
Repeat a random number between 0 and 5 times...
≧×⊕‽θθ
... multiply the number of sides by a random number from 1 to the number of sides.
I⊕‽θ
Print a random number from 1 to the number of sides.
$endgroup$
add a comment
|
$begingroup$
Charcoal, 16 bytes
⊞υ⁶F⊕‽⁶⊞υ⊕‽ΠυI⊟υ
Try it online! Link is to verbose version of code. Explanation:
⊞υ⁶
Push 6 to the predefined list.
F⊕‽⁶
Repeat a random number of times from 1 to 6...
⊞υ⊕‽Πυ
... push a random number between 1 and the product of the list to the list.
I⊟υ
Output the last number pushed to the list.
Alternative approach, also 16 bytes
≔⁶θF‽⁶≧×⊕‽θθI⊕‽θ
Try it online! Link is to verbose version of code. Explanation:
≔⁶θ
Set the number of sides to 6.
F‽⁶
Repeat a random number between 0 and 5 times...
≧×⊕‽θθ
... multiply the number of sides by a random number from 1 to the number of sides.
I⊕‽θ
Print a random number from 1 to the number of sides.
$endgroup$
Charcoal, 16 bytes
⊞υ⁶F⊕‽⁶⊞υ⊕‽ΠυI⊟υ
Try it online! Link is to verbose version of code. Explanation:
⊞υ⁶
Push 6 to the predefined list.
F⊕‽⁶
Repeat a random number of times from 1 to 6...
⊞υ⊕‽Πυ
... push a random number between 1 and the product of the list to the list.
I⊟υ
Output the last number pushed to the list.
Alternative approach, also 16 bytes
≔⁶θF‽⁶≧×⊕‽θθI⊕‽θ
Try it online! Link is to verbose version of code. Explanation:
≔⁶θ
Set the number of sides to 6.
F‽⁶
Repeat a random number between 0 and 5 times...
≧×⊕‽θθ
... multiply the number of sides by a random number from 1 to the number of sides.
I⊕‽θ
Print a random number from 1 to the number of sides.
answered Jun 1 at 9:29
NeilNeil
90.2k8 gold badges46 silver badges190 bronze badges
90.2k8 gold badges46 silver badges190 bronze badges
add a comment
|
add a comment
|
$begingroup$
Python 3, 76 bytes
from random import*
r=randint
a=6
for i in" "*r(0,5):a*=r(1,a)
print(r(1,a))
Try it online!
-2 bytes thanks to TFeld
$endgroup$
1
$begingroup$
-2 bytes by changing the import
$endgroup$
– TFeld
Jun 1 at 7:34
$begingroup$
@TFeld thanks [filler]
$endgroup$
– HyperNeutrino
Jun 1 at 19:23
add a comment
|
$begingroup$
Python 3, 76 bytes
from random import*
r=randint
a=6
for i in" "*r(0,5):a*=r(1,a)
print(r(1,a))
Try it online!
-2 bytes thanks to TFeld
$endgroup$
1
$begingroup$
-2 bytes by changing the import
$endgroup$
– TFeld
Jun 1 at 7:34
$begingroup$
@TFeld thanks [filler]
$endgroup$
– HyperNeutrino
Jun 1 at 19:23
add a comment
|
$begingroup$
Python 3, 76 bytes
from random import*
r=randint
a=6
for i in" "*r(0,5):a*=r(1,a)
print(r(1,a))
Try it online!
-2 bytes thanks to TFeld
$endgroup$
Python 3, 76 bytes
from random import*
r=randint
a=6
for i in" "*r(0,5):a*=r(1,a)
print(r(1,a))
Try it online!
-2 bytes thanks to TFeld
edited Jun 1 at 19:23
answered Jun 1 at 2:54
HyperNeutrinoHyperNeutrino
20.7k4 gold badges41 silver badges155 bronze badges
20.7k4 gold badges41 silver badges155 bronze badges
1
$begingroup$
-2 bytes by changing the import
$endgroup$
– TFeld
Jun 1 at 7:34
$begingroup$
@TFeld thanks [filler]
$endgroup$
– HyperNeutrino
Jun 1 at 19:23
add a comment
|
1
$begingroup$
-2 bytes by changing the import
$endgroup$
– TFeld
Jun 1 at 7:34
$begingroup$
@TFeld thanks [filler]
$endgroup$
– HyperNeutrino
Jun 1 at 19:23
1
1
$begingroup$
-2 bytes by changing the import
$endgroup$
– TFeld
Jun 1 at 7:34
$begingroup$
-2 bytes by changing the import
$endgroup$
– TFeld
Jun 1 at 7:34
$begingroup$
@TFeld thanks [filler]
$endgroup$
– HyperNeutrino
Jun 1 at 19:23
$begingroup$
@TFeld thanks [filler]
$endgroup$
– HyperNeutrino
Jun 1 at 19:23
add a comment
|
$begingroup$
R, 43 bytes
s=sample
for(i in 1:s(k<-6))T=s(k<-k*T,1)
T
Try it online!
k
keeps track of the current number of faces on the die. Uses the fact that T
is initialized as 1
.
I tried a few other things, but couldn't beat this simple, straightforward approach.
$endgroup$
add a comment
|
$begingroup$
R, 43 bytes
s=sample
for(i in 1:s(k<-6))T=s(k<-k*T,1)
T
Try it online!
k
keeps track of the current number of faces on the die. Uses the fact that T
is initialized as 1
.
I tried a few other things, but couldn't beat this simple, straightforward approach.
$endgroup$
add a comment
|
$begingroup$
R, 43 bytes
s=sample
for(i in 1:s(k<-6))T=s(k<-k*T,1)
T
Try it online!
k
keeps track of the current number of faces on the die. Uses the fact that T
is initialized as 1
.
I tried a few other things, but couldn't beat this simple, straightforward approach.
$endgroup$
R, 43 bytes
s=sample
for(i in 1:s(k<-6))T=s(k<-k*T,1)
T
Try it online!
k
keeps track of the current number of faces on the die. Uses the fact that T
is initialized as 1
.
I tried a few other things, but couldn't beat this simple, straightforward approach.
answered Jun 3 at 21:29
Robin RyderRobin Ryder
4,3625 silver badges29 bronze badges
4,3625 silver badges29 bronze badges
add a comment
|
add a comment
|
$begingroup$
Jelly, 10 bytes
6×X$6X’¤¡X
Try it online!
Explanation
¤ | Following as a nilad:
6X | - A random number between 1 and 6
’ | - Decrease by 1 (call this N)
6 | Now, start with 6
$ ¡ | Repeat the following N times, as a monad
× | - Multiply by:
X | - A random number between 1 and the current total
X | Finally, generate a random number between 1 and the output of the above loop
$endgroup$
add a comment
|
$begingroup$
Jelly, 10 bytes
6×X$6X’¤¡X
Try it online!
Explanation
¤ | Following as a nilad:
6X | - A random number between 1 and 6
’ | - Decrease by 1 (call this N)
6 | Now, start with 6
$ ¡ | Repeat the following N times, as a monad
× | - Multiply by:
X | - A random number between 1 and the current total
X | Finally, generate a random number between 1 and the output of the above loop
$endgroup$
add a comment
|
$begingroup$
Jelly, 10 bytes
6×X$6X’¤¡X
Try it online!
Explanation
¤ | Following as a nilad:
6X | - A random number between 1 and 6
’ | - Decrease by 1 (call this N)
6 | Now, start with 6
$ ¡ | Repeat the following N times, as a monad
× | - Multiply by:
X | - A random number between 1 and the current total
X | Finally, generate a random number between 1 and the output of the above loop
$endgroup$
Jelly, 10 bytes
6×X$6X’¤¡X
Try it online!
Explanation
¤ | Following as a nilad:
6X | - A random number between 1 and 6
’ | - Decrease by 1 (call this N)
6 | Now, start with 6
$ ¡ | Repeat the following N times, as a monad
× | - Multiply by:
X | - A random number between 1 and the current total
X | Finally, generate a random number between 1 and the output of the above loop
answered Jun 1 at 6:35
Nick KennedyNick Kennedy
7,3091 gold badge9 silver badges19 bronze badges
7,3091 gold badge9 silver badges19 bronze badges
add a comment
|
add a comment
|
$begingroup$
Ruby, 41 bytes
r=6;rand(2..7).timesr*=$s=rand 1..r;p$s
Try it online!
Explanation
r=6 # Set dice number to 6
rand(2..7).times # Repeat X times, where X=dice roll+1
r*=$s=rand 1..r # Multiply dice number by a dice roll
# Save the most recent dice roll
p$s # Print last dice roll (this is why
# we did the last step one extra time)
$endgroup$
add a comment
|
$begingroup$
Ruby, 41 bytes
r=6;rand(2..7).timesr*=$s=rand 1..r;p$s
Try it online!
Explanation
r=6 # Set dice number to 6
rand(2..7).times # Repeat X times, where X=dice roll+1
r*=$s=rand 1..r # Multiply dice number by a dice roll
# Save the most recent dice roll
p$s # Print last dice roll (this is why
# we did the last step one extra time)
$endgroup$
add a comment
|
$begingroup$
Ruby, 41 bytes
r=6;rand(2..7).timesr*=$s=rand 1..r;p$s
Try it online!
Explanation
r=6 # Set dice number to 6
rand(2..7).times # Repeat X times, where X=dice roll+1
r*=$s=rand 1..r # Multiply dice number by a dice roll
# Save the most recent dice roll
p$s # Print last dice roll (this is why
# we did the last step one extra time)
$endgroup$
Ruby, 41 bytes
r=6;rand(2..7).timesr*=$s=rand 1..r;p$s
Try it online!
Explanation
r=6 # Set dice number to 6
rand(2..7).times # Repeat X times, where X=dice roll+1
r*=$s=rand 1..r # Multiply dice number by a dice roll
# Save the most recent dice roll
p$s # Print last dice roll (this is why
# we did the last step one extra time)
answered Jun 4 at 0:45
Value InkValue Ink
9,5288 silver badges37 bronze badges
9,5288 silver badges37 bronze badges
add a comment
|
add a comment
|
$begingroup$
Java 10, 214 93 86 bytes
v->int r=6,n=0;for(var d=Math.random()*6;d-->0;n*=Math.random(),r*=++n)n=r;return n;
Try it online or try it online with additional print-lines to see the steps.
-128 bytes by using int
instead of java.math.BigInteger
. $6^32$, the largest possible result, doesn't fit inside an int
nor long
, which is why BigInteger
was used initially. OP allowed to use int
and assuming it's infinitely large, so that saved more than 125 bytes here. :) (Here the previous 214 bytes version using BigIntegers
.)
Explanation:
v-> // Method with empty unused parameter & integer return-type
int r=6, // The range in which to roll, starting at 6
n=0; // The roll itself (which must be initialized, therefor is 0)
for(var d=Math.random()*6;// Roll the Prime Bubble Dice
d-->0 // Loop that many times:
; // After every iteration:
n*=Math.random(), // Roll a random dice in the range [0, n)
r*=++n) // Increase `n` by 1 first with `++n`, so the range is [1,n]
// And then multiply `r` by `n` for the new range
n=r; // Set `n` to `r`
return n; // After the loop, return `n` as result
$endgroup$
$begingroup$
Post the solution that doesn't do BigInteger as your competing solution.
$endgroup$
– Stackstuck
Jun 3 at 16:16
$begingroup$
@Stackstuck Will await what OP has to say to your comment. Was about to make one myself to confirm. I personally use BigIntegers/BigDecimals for challenges where it's necessary, unless OP explicitly states you can use your native max integer size (which is usually $2^32$, soint
for both Java and .NET C#).
$endgroup$
– Kevin Cruijssen
Jun 3 at 16:26
$begingroup$
I'll see if I can find anything on meta about this.
$endgroup$
– Stackstuck
Jun 3 at 16:29
1
$begingroup$
OP says don't worry about integer size limits. Use theint
type.
$endgroup$
– Stackstuck
Jun 8 at 1:26
1
$begingroup$
@Stackstuck Done, and golfed 7 bytes in the process. :)
$endgroup$
– Kevin Cruijssen
Jun 8 at 12:00
add a comment
|
$begingroup$
Java 10, 214 93 86 bytes
v->int r=6,n=0;for(var d=Math.random()*6;d-->0;n*=Math.random(),r*=++n)n=r;return n;
Try it online or try it online with additional print-lines to see the steps.
-128 bytes by using int
instead of java.math.BigInteger
. $6^32$, the largest possible result, doesn't fit inside an int
nor long
, which is why BigInteger
was used initially. OP allowed to use int
and assuming it's infinitely large, so that saved more than 125 bytes here. :) (Here the previous 214 bytes version using BigIntegers
.)
Explanation:
v-> // Method with empty unused parameter & integer return-type
int r=6, // The range in which to roll, starting at 6
n=0; // The roll itself (which must be initialized, therefor is 0)
for(var d=Math.random()*6;// Roll the Prime Bubble Dice
d-->0 // Loop that many times:
; // After every iteration:
n*=Math.random(), // Roll a random dice in the range [0, n)
r*=++n) // Increase `n` by 1 first with `++n`, so the range is [1,n]
// And then multiply `r` by `n` for the new range
n=r; // Set `n` to `r`
return n; // After the loop, return `n` as result
$endgroup$
$begingroup$
Post the solution that doesn't do BigInteger as your competing solution.
$endgroup$
– Stackstuck
Jun 3 at 16:16
$begingroup$
@Stackstuck Will await what OP has to say to your comment. Was about to make one myself to confirm. I personally use BigIntegers/BigDecimals for challenges where it's necessary, unless OP explicitly states you can use your native max integer size (which is usually $2^32$, soint
for both Java and .NET C#).
$endgroup$
– Kevin Cruijssen
Jun 3 at 16:26
$begingroup$
I'll see if I can find anything on meta about this.
$endgroup$
– Stackstuck
Jun 3 at 16:29
1
$begingroup$
OP says don't worry about integer size limits. Use theint
type.
$endgroup$
– Stackstuck
Jun 8 at 1:26
1
$begingroup$
@Stackstuck Done, and golfed 7 bytes in the process. :)
$endgroup$
– Kevin Cruijssen
Jun 8 at 12:00
add a comment
|
$begingroup$
Java 10, 214 93 86 bytes
v->int r=6,n=0;for(var d=Math.random()*6;d-->0;n*=Math.random(),r*=++n)n=r;return n;
Try it online or try it online with additional print-lines to see the steps.
-128 bytes by using int
instead of java.math.BigInteger
. $6^32$, the largest possible result, doesn't fit inside an int
nor long
, which is why BigInteger
was used initially. OP allowed to use int
and assuming it's infinitely large, so that saved more than 125 bytes here. :) (Here the previous 214 bytes version using BigIntegers
.)
Explanation:
v-> // Method with empty unused parameter & integer return-type
int r=6, // The range in which to roll, starting at 6
n=0; // The roll itself (which must be initialized, therefor is 0)
for(var d=Math.random()*6;// Roll the Prime Bubble Dice
d-->0 // Loop that many times:
; // After every iteration:
n*=Math.random(), // Roll a random dice in the range [0, n)
r*=++n) // Increase `n` by 1 first with `++n`, so the range is [1,n]
// And then multiply `r` by `n` for the new range
n=r; // Set `n` to `r`
return n; // After the loop, return `n` as result
$endgroup$
Java 10, 214 93 86 bytes
v->int r=6,n=0;for(var d=Math.random()*6;d-->0;n*=Math.random(),r*=++n)n=r;return n;
Try it online or try it online with additional print-lines to see the steps.
-128 bytes by using int
instead of java.math.BigInteger
. $6^32$, the largest possible result, doesn't fit inside an int
nor long
, which is why BigInteger
was used initially. OP allowed to use int
and assuming it's infinitely large, so that saved more than 125 bytes here. :) (Here the previous 214 bytes version using BigIntegers
.)
Explanation:
v-> // Method with empty unused parameter & integer return-type
int r=6, // The range in which to roll, starting at 6
n=0; // The roll itself (which must be initialized, therefor is 0)
for(var d=Math.random()*6;// Roll the Prime Bubble Dice
d-->0 // Loop that many times:
; // After every iteration:
n*=Math.random(), // Roll a random dice in the range [0, n)
r*=++n) // Increase `n` by 1 first with `++n`, so the range is [1,n]
// And then multiply `r` by `n` for the new range
n=r; // Set `n` to `r`
return n; // After the loop, return `n` as result
edited Jun 8 at 12:00
answered Jun 3 at 9:32
Kevin CruijssenKevin Cruijssen
52.4k7 gold badges85 silver badges252 bronze badges
52.4k7 gold badges85 silver badges252 bronze badges
$begingroup$
Post the solution that doesn't do BigInteger as your competing solution.
$endgroup$
– Stackstuck
Jun 3 at 16:16
$begingroup$
@Stackstuck Will await what OP has to say to your comment. Was about to make one myself to confirm. I personally use BigIntegers/BigDecimals for challenges where it's necessary, unless OP explicitly states you can use your native max integer size (which is usually $2^32$, soint
for both Java and .NET C#).
$endgroup$
– Kevin Cruijssen
Jun 3 at 16:26
$begingroup$
I'll see if I can find anything on meta about this.
$endgroup$
– Stackstuck
Jun 3 at 16:29
1
$begingroup$
OP says don't worry about integer size limits. Use theint
type.
$endgroup$
– Stackstuck
Jun 8 at 1:26
1
$begingroup$
@Stackstuck Done, and golfed 7 bytes in the process. :)
$endgroup$
– Kevin Cruijssen
Jun 8 at 12:00
add a comment
|
$begingroup$
Post the solution that doesn't do BigInteger as your competing solution.
$endgroup$
– Stackstuck
Jun 3 at 16:16
$begingroup$
@Stackstuck Will await what OP has to say to your comment. Was about to make one myself to confirm. I personally use BigIntegers/BigDecimals for challenges where it's necessary, unless OP explicitly states you can use your native max integer size (which is usually $2^32$, soint
for both Java and .NET C#).
$endgroup$
– Kevin Cruijssen
Jun 3 at 16:26
$begingroup$
I'll see if I can find anything on meta about this.
$endgroup$
– Stackstuck
Jun 3 at 16:29
1
$begingroup$
OP says don't worry about integer size limits. Use theint
type.
$endgroup$
– Stackstuck
Jun 8 at 1:26
1
$begingroup$
@Stackstuck Done, and golfed 7 bytes in the process. :)
$endgroup$
– Kevin Cruijssen
Jun 8 at 12:00
$begingroup$
Post the solution that doesn't do BigInteger as your competing solution.
$endgroup$
– Stackstuck
Jun 3 at 16:16
$begingroup$
Post the solution that doesn't do BigInteger as your competing solution.
$endgroup$
– Stackstuck
Jun 3 at 16:16
$begingroup$
@Stackstuck Will await what OP has to say to your comment. Was about to make one myself to confirm. I personally use BigIntegers/BigDecimals for challenges where it's necessary, unless OP explicitly states you can use your native max integer size (which is usually $2^32$, so
int
for both Java and .NET C#).$endgroup$
– Kevin Cruijssen
Jun 3 at 16:26
$begingroup$
@Stackstuck Will await what OP has to say to your comment. Was about to make one myself to confirm. I personally use BigIntegers/BigDecimals for challenges where it's necessary, unless OP explicitly states you can use your native max integer size (which is usually $2^32$, so
int
for both Java and .NET C#).$endgroup$
– Kevin Cruijssen
Jun 3 at 16:26
$begingroup$
I'll see if I can find anything on meta about this.
$endgroup$
– Stackstuck
Jun 3 at 16:29
$begingroup$
I'll see if I can find anything on meta about this.
$endgroup$
– Stackstuck
Jun 3 at 16:29
1
1
$begingroup$
OP says don't worry about integer size limits. Use the
int
type.$endgroup$
– Stackstuck
Jun 8 at 1:26
$begingroup$
OP says don't worry about integer size limits. Use the
int
type.$endgroup$
– Stackstuck
Jun 8 at 1:26
1
1
$begingroup$
@Stackstuck Done, and golfed 7 bytes in the process. :)
$endgroup$
– Kevin Cruijssen
Jun 8 at 12:00
$begingroup$
@Stackstuck Done, and golfed 7 bytes in the process. :)
$endgroup$
– Kevin Cruijssen
Jun 8 at 12:00
add a comment
|
$begingroup$
PHP, 59 bytes
$r=$q=rand(1,$s=6);while($l++<$q)$r=rand(1,$s*=$r);print$r;
expanded:
$r=$q=rand(1,$s=6);
while($l++<$q)$
r=rand(1,$s*=$r);
print$r;
Not sure if I'm supposed to include the open tag.
On my machine, it crashes if $s*$r
is too large, so it doesn't print on $q>=5
sometimes... because the numbers get so big. Not sure of a fix.
$endgroup$
add a comment
|
$begingroup$
PHP, 59 bytes
$r=$q=rand(1,$s=6);while($l++<$q)$r=rand(1,$s*=$r);print$r;
expanded:
$r=$q=rand(1,$s=6);
while($l++<$q)$
r=rand(1,$s*=$r);
print$r;
Not sure if I'm supposed to include the open tag.
On my machine, it crashes if $s*$r
is too large, so it doesn't print on $q>=5
sometimes... because the numbers get so big. Not sure of a fix.
$endgroup$
add a comment
|
$begingroup$
PHP, 59 bytes
$r=$q=rand(1,$s=6);while($l++<$q)$r=rand(1,$s*=$r);print$r;
expanded:
$r=$q=rand(1,$s=6);
while($l++<$q)$
r=rand(1,$s*=$r);
print$r;
Not sure if I'm supposed to include the open tag.
On my machine, it crashes if $s*$r
is too large, so it doesn't print on $q>=5
sometimes... because the numbers get so big. Not sure of a fix.
$endgroup$
PHP, 59 bytes
$r=$q=rand(1,$s=6);while($l++<$q)$r=rand(1,$s*=$r);print$r;
expanded:
$r=$q=rand(1,$s=6);
while($l++<$q)$
r=rand(1,$s*=$r);
print$r;
Not sure if I'm supposed to include the open tag.
On my machine, it crashes if $s*$r
is too large, so it doesn't print on $q>=5
sometimes... because the numbers get so big. Not sure of a fix.
answered Jun 2 at 5:00
ReedReed
1414 bronze badges
1414 bronze badges
add a comment
|
add a comment
|
$begingroup$
Pyth, 14 bytes
uhO=*|Z6GO6hO6
Try it online!
uhO=*|Z6GO6hO6
O6 Random number in range [0-6)
u Perform the following the above number of times...
hO6 ... with starting value G a random number in range [1-6]:
* G Multiply G with...
|Z6 The value of Z, or 6 if it's the first time through (Z is 0 at program start)
= Z Assign the above back into Z
O Random number in range [0-Z)
h Increment
Implicit print result of final iteration
$endgroup$
add a comment
|
$begingroup$
Pyth, 14 bytes
uhO=*|Z6GO6hO6
Try it online!
uhO=*|Z6GO6hO6
O6 Random number in range [0-6)
u Perform the following the above number of times...
hO6 ... with starting value G a random number in range [1-6]:
* G Multiply G with...
|Z6 The value of Z, or 6 if it's the first time through (Z is 0 at program start)
= Z Assign the above back into Z
O Random number in range [0-Z)
h Increment
Implicit print result of final iteration
$endgroup$
add a comment
|
$begingroup$
Pyth, 14 bytes
uhO=*|Z6GO6hO6
Try it online!
uhO=*|Z6GO6hO6
O6 Random number in range [0-6)
u Perform the following the above number of times...
hO6 ... with starting value G a random number in range [1-6]:
* G Multiply G with...
|Z6 The value of Z, or 6 if it's the first time through (Z is 0 at program start)
= Z Assign the above back into Z
O Random number in range [0-Z)
h Increment
Implicit print result of final iteration
$endgroup$
Pyth, 14 bytes
uhO=*|Z6GO6hO6
Try it online!
uhO=*|Z6GO6hO6
O6 Random number in range [0-6)
u Perform the following the above number of times...
hO6 ... with starting value G a random number in range [1-6]:
* G Multiply G with...
|Z6 The value of Z, or 6 if it's the first time through (Z is 0 at program start)
= Z Assign the above back into Z
O Random number in range [0-Z)
h Increment
Implicit print result of final iteration
edited Jun 3 at 8:53
answered Jun 3 at 8:12
SokSok
4,87711 silver badges28 bronze badges
4,87711 silver badges28 bronze badges
add a comment
|
add a comment
|
$begingroup$
C# (.NET Core), 136 bytes
class Astatic void Main()var r=new System.Random();int i=r.Next(6),j=6;while(i-->0)j*=1+r.Next(j);System.Console.Write(r.Next(j)+1);
Try it online!
I'm pretty sure this works, given the assumption of infinite integer length that we're fond of here. If I have to actually handle the overflow, I'd need to bust out an entirely different class.
$endgroup$
$begingroup$
It can now give aSystem.ArgumentOutOfRangeException: 'maxValue' must be greater than zero
error. The max result can be $6^32$, which is larger than the max size of bothint
andlong
, so you'll have to useBigIntegers
instead.
$endgroup$
– Kevin Cruijssen
Jun 3 at 9:34
$begingroup$
@KevinCruijssen yes, that's the whole point of my comment.
$endgroup$
– Stackstuck
Jun 3 at 16:12
add a comment
|
$begingroup$
C# (.NET Core), 136 bytes
class Astatic void Main()var r=new System.Random();int i=r.Next(6),j=6;while(i-->0)j*=1+r.Next(j);System.Console.Write(r.Next(j)+1);
Try it online!
I'm pretty sure this works, given the assumption of infinite integer length that we're fond of here. If I have to actually handle the overflow, I'd need to bust out an entirely different class.
$endgroup$
$begingroup$
It can now give aSystem.ArgumentOutOfRangeException: 'maxValue' must be greater than zero
error. The max result can be $6^32$, which is larger than the max size of bothint
andlong
, so you'll have to useBigIntegers
instead.
$endgroup$
– Kevin Cruijssen
Jun 3 at 9:34
$begingroup$
@KevinCruijssen yes, that's the whole point of my comment.
$endgroup$
– Stackstuck
Jun 3 at 16:12
add a comment
|
$begingroup$
C# (.NET Core), 136 bytes
class Astatic void Main()var r=new System.Random();int i=r.Next(6),j=6;while(i-->0)j*=1+r.Next(j);System.Console.Write(r.Next(j)+1);
Try it online!
I'm pretty sure this works, given the assumption of infinite integer length that we're fond of here. If I have to actually handle the overflow, I'd need to bust out an entirely different class.
$endgroup$
C# (.NET Core), 136 bytes
class Astatic void Main()var r=new System.Random();int i=r.Next(6),j=6;while(i-->0)j*=1+r.Next(j);System.Console.Write(r.Next(j)+1);
Try it online!
I'm pretty sure this works, given the assumption of infinite integer length that we're fond of here. If I have to actually handle the overflow, I'd need to bust out an entirely different class.
edited Jun 3 at 16:18
answered Jun 2 at 19:54
StackstuckStackstuck
2091 silver badge7 bronze badges
2091 silver badge7 bronze badges
$begingroup$
It can now give aSystem.ArgumentOutOfRangeException: 'maxValue' must be greater than zero
error. The max result can be $6^32$, which is larger than the max size of bothint
andlong
, so you'll have to useBigIntegers
instead.
$endgroup$
– Kevin Cruijssen
Jun 3 at 9:34
$begingroup$
@KevinCruijssen yes, that's the whole point of my comment.
$endgroup$
– Stackstuck
Jun 3 at 16:12
add a comment
|
$begingroup$
It can now give aSystem.ArgumentOutOfRangeException: 'maxValue' must be greater than zero
error. The max result can be $6^32$, which is larger than the max size of bothint
andlong
, so you'll have to useBigIntegers
instead.
$endgroup$
– Kevin Cruijssen
Jun 3 at 9:34
$begingroup$
@KevinCruijssen yes, that's the whole point of my comment.
$endgroup$
– Stackstuck
Jun 3 at 16:12
$begingroup$
It can now give a
System.ArgumentOutOfRangeException: 'maxValue' must be greater than zero
error. The max result can be $6^32$, which is larger than the max size of both int
and long
, so you'll have to use BigIntegers
instead.$endgroup$
– Kevin Cruijssen
Jun 3 at 9:34
$begingroup$
It can now give a
System.ArgumentOutOfRangeException: 'maxValue' must be greater than zero
error. The max result can be $6^32$, which is larger than the max size of both int
and long
, so you'll have to use BigIntegers
instead.$endgroup$
– Kevin Cruijssen
Jun 3 at 9:34
$begingroup$
@KevinCruijssen yes, that's the whole point of my comment.
$endgroup$
– Stackstuck
Jun 3 at 16:12
$begingroup$
@KevinCruijssen yes, that's the whole point of my comment.
$endgroup$
– Stackstuck
Jun 3 at 16:12
add a comment
|
$begingroup$
Julia 1.0, 60 bytes
g(b=big(6),r=rand)=(for i in 1:r(0:5) b=b*r(1:b) end;r(1:b))
b=big(6)
makes it work with arbitrary sized integers
Try it online!
$endgroup$
add a comment
|
$begingroup$
Julia 1.0, 60 bytes
g(b=big(6),r=rand)=(for i in 1:r(0:5) b=b*r(1:b) end;r(1:b))
b=big(6)
makes it work with arbitrary sized integers
Try it online!
$endgroup$
add a comment
|
$begingroup$
Julia 1.0, 60 bytes
g(b=big(6),r=rand)=(for i in 1:r(0:5) b=b*r(1:b) end;r(1:b))
b=big(6)
makes it work with arbitrary sized integers
Try it online!
$endgroup$
Julia 1.0, 60 bytes
g(b=big(6),r=rand)=(for i in 1:r(0:5) b=b*r(1:b) end;r(1:b))
b=big(6)
makes it work with arbitrary sized integers
Try it online!
answered Jun 3 at 17:36
gggggggg
1,3946 silver badges8 bronze badges
1,3946 silver badges8 bronze badges
add a comment
|
add a comment
|
$begingroup$
Gaia, 13 bytes
:(ṛ×
6₅ṛ(↑ₓ(ṛ
Try it online!
$endgroup$
add a comment
|
$begingroup$
Gaia, 13 bytes
:(ṛ×
6₅ṛ(↑ₓ(ṛ
Try it online!
$endgroup$
add a comment
|
$begingroup$
Gaia, 13 bytes
:(ṛ×
6₅ṛ(↑ₓ(ṛ
Try it online!
$endgroup$
Gaia, 13 bytes
:(ṛ×
6₅ṛ(↑ₓ(ṛ
Try it online!
edited Jun 3 at 19:12
answered Jun 3 at 15:53
GiuseppeGiuseppe
19.4k3 gold badges16 silver badges71 bronze badges
19.4k3 gold badges16 silver badges71 bronze badges
add a comment
|
add a comment
|
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f186294%2fimplement-homestucks-catenative-doomsday-dice-cascader%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
$begingroup$
Whats the maximum output? If every roll results in the maximum side? I think it's 7958661109946400884391936 = ((((6^2)^2)^2)^2)^2 = 6^(2^5) = 6^32
$endgroup$
– Kjetil S.
Jun 1 at 11:53
6
$begingroup$
@KjetilS. Indeed, and the likelihood of that output should be $frac16*prod_i=0^5(6^2^i)=frac16times 6times 6^2times 6^4times 6^8times 6^16times 6^32=frac12155416739906037495048372267884096782336$
$endgroup$
– Jonathan Allan
Jun 1 at 12:50
3
$begingroup$
Is this supposed to be random? The question doesn't mention anything about randomness at all?
$endgroup$
– Sriotchilism O'Zaic
Jun 1 at 16:35
10
$begingroup$
@SriotchilismO'Zaic Dice-rolling implies randomness.
$endgroup$
– mbomb007
Jun 1 at 21:34
6
$begingroup$
@SriotchilismO'Zaic xkcd.com/221
$endgroup$
– Neyt
Jun 3 at 9:52