Skip to main content
added 190 characters in body
Source Link
Galen Ivanov
  • 21.5k
  • 3
  • 26
  • 62

J, 13 bytes

[:}:? ::]^:a: 

On the subway, so apologies for lack of TIO (hopefully there isn’t a lack of correctness).

Outputs a list of values.

Presumably the APL approach will be shorter, but this is what I thought of.

How it works

^:a: apply repeatedly until convergence, storing intermediate results in an array.

? random integer in range [0, K) for K greater than 0. For 0, it gives a random integer in range (0,1). For a floating point number, it errors.

::] catch an error for an input to ? and instead of erroring, output the input that caused the error.

}: get rid of the last value in the array (this is so that a floating point number isn’t output).

Try it online!

J, 13 bytes

[:}:? ::]^:a: 

On the subway, so apologies for lack of TIO (hopefully there isn’t a lack of correctness).

Outputs a list of values.

Presumably the APL approach will be shorter, but this is what I thought of.

How it works

^:a: apply repeatedly until convergence, storing intermediate results in an array.

? random integer in range [0, K) for K greater than 0. For 0, it gives a random integer in range (0,1). For a floating point number, it errors.

::] catch an error for an input to ? and instead of erroring, output the input that caused the error.

}: get rid of the last value in the array (this is so that a floating point number isn’t output).

J, 13 bytes

[:}:? ::]^:a: 

On the subway, so apologies for lack of TIO (hopefully there isn’t a lack of correctness).

Outputs a list of values.

Presumably the APL approach will be shorter, but this is what I thought of.

How it works

^:a: apply repeatedly until convergence, storing intermediate results in an array.

? random integer in range [0, K) for K greater than 0. For 0, it gives a random integer in range (0,1). For a floating point number, it errors.

::] catch an error for an input to ? and instead of erroring, output the input that caused the error.

}: get rid of the last value in the array (this is so that a floating point number isn’t output).

Try it online!

Source Link
cole
  • 4k
  • 1
  • 14
  • 26

J, 13 bytes

[:}:? ::]^:a: 

On the subway, so apologies for lack of TIO (hopefully there isn’t a lack of correctness).

Outputs a list of values.

Presumably the APL approach will be shorter, but this is what I thought of.

How it works

^:a: apply repeatedly until convergence, storing intermediate results in an array.

? random integer in range [0, K) for K greater than 0. For 0, it gives a random integer in range (0,1). For a floating point number, it errors.

::] catch an error for an input to ? and instead of erroring, output the input that caused the error.

}: get rid of the last value in the array (this is so that a floating point number isn’t output).