Skip to main content
added 1 character in body
Source Link
Luis Mendo
  • 106.7k
  • 10
  • 139
  • 382

MATL, 5 bytes

`1rEk 

Outputs 1 separated by newlines n times, where n is a (shifted) geometric random variable with parameter 1/2. This means n is 1,2,3... with probability 1/2,1/4, 1/8 ... The proramprogram halts with probability 1.

Try it online!

` % Do...while 1 % Push 1 r % Push uniform random number in the interval (0,1) E % Multiply by 2 k % Round down. This gives 0 or 1 with probability 1/2 % End (implicit). The top of the stack is used as loop condition % If 1 a new iteration is executed, otherwise the loop is exited % Display stack (implicit) 

MATL, 5 bytes

`1rEk 

Outputs 1 separated by newlines n times, where n is a (shifted) geometric random variable with parameter 1/2. This means n is 1,2,3... with probability 1/2,1/4, 1/8 ... The proram halts with probability 1.

Try it online!

` % Do...while 1 % Push 1 r % Push uniform random number in the interval (0,1) E % Multiply by 2 k % Round down. This gives 0 or 1 with probability 1/2 % End (implicit). The top of the stack is used as loop condition % If 1 a new iteration is executed, otherwise the loop is exited % Display stack (implicit) 

MATL, 5 bytes

`1rEk 

Outputs 1 separated by newlines n times, where n is a (shifted) geometric random variable with parameter 1/2. This means n is 1,2,3... with probability 1/2,1/4, 1/8 ... The program halts with probability 1.

Try it online!

` % Do...while 1 % Push 1 r % Push uniform random number in the interval (0,1) E % Multiply by 2 k % Round down. This gives 0 or 1 with probability 1/2 % End (implicit). The top of the stack is used as loop condition % If 1 a new iteration is executed, otherwise the loop is exited % Display stack (implicit) 
Source Link
Luis Mendo
  • 106.7k
  • 10
  • 139
  • 382

MATL, 5 bytes

`1rEk 

Outputs 1 separated by newlines n times, where n is a (shifted) geometric random variable with parameter 1/2. This means n is 1,2,3... with probability 1/2,1/4, 1/8 ... The proram halts with probability 1.

Try it online!

` % Do...while 1 % Push 1 r % Push uniform random number in the interval (0,1) E % Multiply by 2 k % Round down. This gives 0 or 1 with probability 1/2 % End (implicit). The top of the stack is used as loop condition % If 1 a new iteration is executed, otherwise the loop is exited % Display stack (implicit)