29
\$\begingroup\$

Challenge

Sandbox post

Given a positive integer (K) Output a uniformly-random integer (Y) between [0, K).

If Y > 0 Assume K = Y and repeat the process until Y = 0.

Rules

  • Input must be printed at first
  • Output format as you wish
  • Your program must finish.
  • 0 must be the final output, Optionally an empty line instead 0
\$\endgroup\$
  • \$\begingroup\$ If the submission is a function, may it return 0 in addition to printing it? \$\endgroup\$ – Adám Jul 9 '18 at 12:55
  • 1
    \$\begingroup\$ @Adám yes, you can return in addition \$\endgroup\$ – Luis felipe De jesus Munoz Jul 9 '18 at 12:59
  • \$\begingroup\$ Do I need to seed my RNG? \$\endgroup\$ – SIGSTACKFAULT Jul 9 '18 at 16:16
  • \$\begingroup\$ May we print without delimiters? \$\endgroup\$ – Titus Jul 30 '18 at 15:44
  • \$\begingroup\$ I got curious. It's quite easy to prove that the average number of steps this program takes before it terminates is H(K-1) + 1 where H(K) is the K'th harmonic number. For n=1000, that's 8.484 steps on average. \$\endgroup\$ – J.Doe Sep 21 '18 at 14:02

69 Answers 69

1
\$\begingroup\$

Elixir, 87 bytes

fn n->[n]++Enum.take_while(Stream.repeatedly(fn->Enum.random(0..n)end),&(&1>1))++[0]end

Try it online!

\$\endgroup\$
  • \$\begingroup\$ This isn't actually valid as an answer - it doesn't uniformly randomize, the sequence is strictly decreasing due to how you recurse (if your first iteration is 50>25, it can never generate numbers 26-50 again). \$\endgroup\$ – Delioth Jul 30 '18 at 13:50
  • \$\begingroup\$ @Delioth fixed. \$\endgroup\$ – Okx Jul 30 '18 at 14:13
1
\$\begingroup\$

PHP, 44 bytes

<?=$a=$argn;while($a--)echo _,$a=rand(0,$a);

or

<?for(;$a=&$argn;$a=rand(0,$a))echo$a--,_?>0

print integers delimited by underscore. Save to file, run as pipe with -nF.

\$\endgroup\$
1
\$\begingroup\$

Noether, 9 bytes

I~n0(nRP)

Try it online!

Output is given in the form

735920

Explanation:

I~n        - Store input in the variable n
   0(   ) - Loop until the top of the stack equals zero
     nR    - Push a random integer between 0 and n
       P   - Print the top of the stack
\$\endgroup\$
1
\$\begingroup\$

Swift 4, 30 bytes

while n>0{n=rand()%n;print(n)}

Try it online!

n is the value of input

\$\endgroup\$
1
\$\begingroup\$

Forth (gforth), 57 bytes

include random.fs
: f begin dup . random dup 0= until . ;

Try it online!

Explanation

Get Random integer in range, loop until result is 0.

Code Explanation

include random.fs     \ import the file that implements the "random" word
:f                    \ start a word defintion
  begin               \ start an indefinite loop
    dup .             \ duplicate the top of the stack and print it
    random            \ get random int between 0 and n - 1
    dup 0=            \ duplicate the top of the stack and then check if it's 0
  until               \ if it's zero end the loop
  .                   \ output the top of the stack (always 0)
;                     \ end the word definition
\$\endgroup\$
1
\$\begingroup\$

Julia 1.0, 53 bytes

function q(N)println(N);if N>0;q(rand(0:N-1));end;end

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Pyt, 5 bytes

`0⇹ɾł

Try it online!

Explanation:

         Implicit input
`   ł    Do ... while top of stack is not 0:
 0          Push 0
  ⇹         Swap top two elements on stack
   ɾ        Get random number in [0,top of stack)
         Implicit print
\$\endgroup\$
  • \$\begingroup\$ At the TIO link, this always just outputs 0. Am I missing something? \$\endgroup\$ – DLosc Sep 23 '18 at 4:22
1
\$\begingroup\$

Fortran 90, 56 bytes

read(*,*)i
do while(i>0)
i=rand(i)*i
print*,i
enddo
end

Trying to golf Fortran is always fun.

Also, because seeding the RNG in Fortran is a pain and costs a lot of bytes (and RAND() uses a particularly poor algorithm but has a nice short name), the output is pretty much deterministic - but I'm hoping to get away with it.

Example

Input:

10000000

Output:

 2636923
 1681125
  264113
   17707
    2453
      47
       0
\$\endgroup\$
0
\$\begingroup\$

PHP 5.6

$k = $y = 5; do { echo $k. "\n"; $k = rand(0, $y-1); } while($k > 0)

\$\endgroup\$
  • 2
    \$\begingroup\$ Please specify the name (and if relevant the version) of the used language in a Header at top of your answer, together with the code length measured in bytes. And please use Code and Preformatted Text markup for the code block to make it readable. Then please read the code-golf tag wiki about what is the goal in these challenges. \$\endgroup\$ – manatwork Sep 21 '18 at 12:39
  • 1
    \$\begingroup\$ To help getting into golfing, there is a collection of Tips for golfing in PHP. And also a generic Tips for golfing in <all languages>. \$\endgroup\$ – manatwork Sep 21 '18 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.