Riddler: Simulating a Non-increasing Sequence

Simulation

Published

April 5, 2020

Notable topics: Simulation

Recorded on: 2020-04-05

Timestamps by: Alex Cookson

View code

Screencast

Timestamps

accumulate

Introducing accumulate functon as a possible solution (but not used here)

sample

Using sample function to simulate 1000 rolls of a 10-sided die

Explanation of dividing sample rolls into streaks (instead of using logic similar to a while loop)

cumsum

Using cumsum function to separate 1000 rolls into individual sequences (which end when a 0 is rolled)

lag

Using lag function to "shift" sequence numbering down by one row

cummaxlag

Using cummax and lag functions to check whether a roll is less than the highest value rolled previously in the sequence

cummin

Fixing previous step with cummin function (instead of cummax) and dropping the lag function

Finished simulation code and starting to calculate scores

row_number

Using -row_number function (note the minus sign!) to calculate decimal position of number in the score

Investigating the distribution of scores

seqscale_x_continuous

Using seq function in the breaks argument of scale_x_continuous to set custom, evenly-spaced axis ticks and labels