# Print random integers until 0

You are to write a program which generates random integers between $$\0\$$ and $$\99\$$ inclusive, outputting each integer in turn, until $$\0\$$ is generated. You may choose which single-order random distribution (uniform, binomial, Poisson etc.) you use so long as each integer has a non-zero chance of being generated and is chosen independently. The output should always end with 0. As each integer must be chosen independently, the output cannot be some permutation of the integers $$\\{0, 1, 2, ..., 99\}\$$ trimmed to end with $$\0\$$.

You may follow another method to accomplish the same task, so long as the result is identical to the described method here (for example: you may generate a number $$\K\$$ geometrically distributed with parameter $$\\frac 1 {99}\$$, then output $$\K\$$ independent numbers with a uniform distribution on the set $$\\{1, 2, ..., 99\}\$$, then output a $$\0\$$).

The integers may be separated by any non-digit, non-empty separator (e.g. newlines, spaces etc.), and may be output in any consistent base. You may output in any convenient method or format.

This is so the shortest code in bytes wins.

• @KevinCruijssen No, the integers do not have to be unique (aside from 0, which should appear exactly once), and yes, you may output them as a list Jan 28 at 14:52
• Surely if the integers must be chosen independently, then the output cannot be unique? Jan 28 at 15:50
• @cairdcoinheringaahing for example, if a 5 is chosen, the chance of choosing another 5 has changed to 0. That isn't independent. Jan 28 at 16:06
• @pxeger FWIW I agree with pxeger. The output numbers being unique is not compatible with independence Jan 28 at 16:15
• @LuisMendo Yep, that’s fine Jan 28 at 16:40

# Orst, 25 bytes

{100ZЉΞªκ}{ªΐŌ}Ꮳΐ


Try it Online!

## Explained

{100ZЉΞªκ}{ªΐŌ}Ꮳΐ
Ꮳ   # While:
{100ZЉ             #   A random number chosen from the range [0, 100] is truthy
Ξªκ          #   And stored in variable y
}
{ªΐŌ}    # Print that number
ΐ  # Push 0 and print that too



# Zsh + coreutils, 24 bytes

shuf -i0-99|grep -xB99 0


Try it online!

shuffle the integers 0-99, then search (grep) for a line that exactly matches the pattern 0, outputting the 99 lines Before that.

• zsh - or pretty much any other shell. Jan 28 at 16:18
• @DigitalTrauma I just use zsh because I have it bookmarked on TIO :q Jan 28 at 16:20

# TSQL, 52 bytes

DECLARE @ INT A:SET @=RAND()*100PRINT @;IF @>0GOTO A


Probably shorter ways to do this. Declaring the variable as an integer means we don't have to explicitly call a ROUND or FLOOR function. A GOTO loop is shorter than a WHILE loop.

# Bash, 60 bytes

while true;do echo $(($RANDOM%100));done|grep -B99999 -m1 ^0


Try it online!

• 44, no external programs: tio.run/##S0oszvj/vzwjMydVITU5I19BRUMj0TbI0c/… Although I'm fairly certain $RANDOM doesn't qualify here, it is deterministic after the first call. Jan 30 at 11:02 • -1 byte: $(($RANDOM%100)) -> $((RANDOM%100)) Jan 31 at 7:29
• -9 bytes: for((;;)){ echo $[RANDOM%100];}|grep -B99999 -m1 ^0 .. test it Mar 21 at 8:32 # Wolfram Language (Mathematica), 30 bytes While[Echo@RandomInteger@99>0]  Try it online! # Icon, 39 bytes procedure f() write(r:=|?100-1)&r=0 end  Try it online! Please note that link random is needed for the randomize() function, because the output in TIO is always the same. The random function in Icon is ? ## Explanation: ?100 returns a random integer in the range 1..100 |?100 turns it into a generator |?100-1 subtract 1, so that the random number is in the range 0..99 r:=|?100-1 assign the value to a variable r write(r:=|?100-1) prints the value &r=0 if r is not zero, backtrack and generate a new value # Pyth, 10 bytes WnTZ=TO99T  Try it online! Explanation: W # while loop nTZ # compare T (initially 10) not equal to Z (initially 0) =TO99 # assign T to a random number from 0 to 99 T # output T  # Julia, 27 bytes while 0<@show rand(0:99)end  Try it online! # PowerShell, 4536 31 bytes $x=1;while($x){($x=random 100)}


Try it online!

Shameless port of Python 3 verbose answer by @Gotoro!

-5 bytes thanks to @Zaelin

• You can save a byte by using a (mostly empty) for loop rather than a while loop, another byte by implicitly outputting x by wrapping its assignment in parenthesis, and 4 more bytes by implicitly aliasing get-random -> random. Try it online! Jan 29 at 19:13
• And you can save an additional 1 byte in Powershell 7 (unfortunately, TIO uses 6), by using the null-coalescing operator ?? like so: for(;$x ??1){($x=random 99)} Jan 29 at 19:20
• @ZaelinGoodman thanks for it Jan 30 at 2:17

# Zsh,54 47 bytes

-1 bytes by using /dev/random (@EasyasPi), -2 bytes by using $_ instead of assigning a new parameter, -4 bytes by reading a line instead of a single character. read -u0 c</dev/random echo$[#c%100]
((_))&&$0  1 2 3 Try it online! (uses /dev/urandom instead due to TIO's jail) Bash and Zsh's $RANDOM does not qualify as an independent RNG. The first call is random, but subsequent calls are intentionally deterministic. So we have to make our own, reading a line from /dev/random. We get the codepoint of the first character with #c.

Finally, the $_ parameter is the last argument of the previous command, which saves us having to do $[x=#c%100].

The TIO link uses a function, but this will also work as a standalone program (just mind the recursion)

• -1 byte for /dev/random, although TIO's jail artificially prevents accessing it. Jan 30 at 17:30

## Javascript (JQuery) 31 bytes

while(alert(a=$.now()%100)||a); <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> • Could you edit in a link to an online interpreter such as Tryitonline! so that others can test your program? Jan 28 at 16:38 • idk how to get jquery working on TIO. Jan 28 at 16:50 • $.now() is just an alias for Date.now(), so you can easily define it in the header section of TIO. Same thing for alert(). Note that extra code in the header and footer sections does not count towards your code size. Here is an example. That said, in this particular case, using console.log() instead of alert() does change the behavior, as you lose the entropy provided by the user input (by closing the alert popup). Jan 28 at 21:43
• Is using $.now() generate random number multiple times allowed on this site? – tsh Jan 29 at 2:03 • Can it ever generate 99? – l4m2 Mar 16 at 20:35 # Vyxal, 6 bytes {₁ʁ℅…|  Try it Online! I'm actually quite happy with the way this one turned out. It's always fun when you can print things in the loop's condition. ## Explained {₁ʁ℅…| { # Open a while loop which loops while: ₁ʁ # From the range [0, 100) ℅ # A randomly chosen item is truthy (non-0) … # (Print that value without popping it) | # Branch to the empty body of the while loop  In summary, this works by randomly choosing and printing a random number in the range [0, 100) until that value is 0. # Red, 32 bytes until[0 = probe -1 + random 100]  Try it online! • Note that, as Red is the Language of the Month, it's recommended that you edit your answers into the list of answers so that we get a rough metric of how well used Red was this month :) Jun 4 at 14:04 ## Octave/MATLAB with Statistics Package/Toolbox, 31 29 bytes @()[randi(99,1,geornd(.1)) 0]  Anonymous function that takes no inputs and outputs a row vector. This uses a modification of the approach described in the second paragraph of the challenge, with the probability of 0 being 1/10 and the probability of each of 1, 2, ... , 99 being 1/110. Try it online! # Charcoal, 10 bytes Ｗ‽¹⁰⁰⟦Ｉι⟧0  Try it online! Link is to verbose version of code. Explanation: Ｗ‽¹⁰⁰  Repeat while a randomly chosen integer from 0 to 99 is non-zero... ⟦Ｉι⟧  ... print the integer on its own line. (Appending a separator doesn't help because the string needs to be separated from the following code.) 0  Print a 0. # SQLite, 156 bytes with t as (select 1 x union all select abs(random())%100 from t limit 1e5), s as (select group_concat(x) s from t) select substr(s,3,instr(s,',0,')-1) from s  Try it online! # Scala, 65 bytes def f=Seq.fill(99999)(util.Random.nextInt(100)).takeWhile(_>0):+0  Try it online! # Nim, 61 60 bytes import random randomize() var x=1 while x>0:x=rand 99;echo x  Try it online! -1 byte thanks to Danis • x!=0 --> x>0 Jan 28 at 20:34 # F#, 72 70 bytes -2 bytes: changed number from "a= -1" to "a=1" (typo) let mutable a=1 while a<>0 do a<-System.Random().Next(99);printfn"%i"a  Try it online! There probably would be a way of doing this in a more functional manner, but this is what I've come up with. # Befunge-98 (Rc/Funge-98) + RAND fingerprint, 17 bytes #@_"DNAR"4('dI:.!  It doesn't work on TIO with FBBI nor with PyFunge because they don't have the RAND fingerprint implemented. # Racket, 58 bytes (define(f[r(random 100)])(and(writeln r)(when(< 0 r)(f))))  Try it online! # K (oK), 18 16 bytes (*|){x,1?100}/""  Try it online! # Haskell, 67 bytes Creates a monad containing the random list import System.Random m=randomRIO(0,99)>>=k k 0=pure[0] k x=(x:)<$>m


Try it online!

import System.Random