# 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, 2021 at 14:52
• Surely if the integers must be chosen independently, then the output cannot be unique? Jan 28, 2021 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, 2021 at 16:06
• @pxeger FWIW I agree with pxeger. The output numbers being unique is not compatible with independence Jan 28, 2021 at 16:15
• @LuisMendo Yep, that’s fine Jan 28, 2021 at 16:40

# Orst, 25 bytes

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


Try it Online!

Yes, Caird, I used one of your esolangs to answer your challenge.

## 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, 2021 at 16:18
• @DigitalTrauma I just use zsh because I have it bookmarked on TIO :q Jan 28, 2021 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, 2021 at 11:02 • -1 byte: $(($RANDOM%100)) -> $((RANDOM%100)) Jan 31, 2021 at 7:29

# 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!

$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, 2021 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, 2021 at 19:20 • @ZaelinGoodman thanks for it Jan 30, 2021 at 2:17 • Down to 28 using do...while Jan 31, 2023 at 1:49 # 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, 2021 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, 2021 at 16:38
• idk how to get jquery working on TIO. Jan 28, 2021 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, 2021 at 21:43 • Is using $.now() generate random number multiple times allowed on this site?
– tsh
Jan 29, 2021 at 2:03
• Can it ever generate 99?
– l4m2
Mar 16, 2021 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, 2021 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, 2021 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! # Haskell, 80 bytes Complete program import System.Random main=randomRIO(0,99::Int)>>=k;k 0=print 0;k x=print x>>main  Try it online! # TI-BASIC, 14 bytes Repeat not(Ans int(E2rand Disp Ans End  Uses E2 as a shortcut for 10^2=100 to save a byte along with int(rand to save bytes over randInt( - Ans also makes a useful appearance # ARM Thumb-2 machine code (Linux, no libc/div), 34 bytes Machine code: 4668 2101 2203 01d7 df00 4669 2201 2704 780d 3d64 d8fd 3564 700d df00 d1f0 2701 df00  Commented assembly:  .syntax unified .arch armv6t2 // technically ARMv4T compatible .thumb .globl _start .thumb_func _start: .Lloop: // It sucks that this takes the buffer in r0. :( // buf = sp mov r0, sp // size = 1 movs r1, #1 // flags = GRND_RANDOM | GRND_NOBLOCK = 3 // 0 is preferred, but this allows us to cheat loading the getrandom // syscall which won't fit in a narrow movs. movs r2, #3 // getrandom = 0x180 = 3 << 7 // no movw required // movw r7, #0x180 lsls r7, r2, #7 // getrandom(&x, 1, GRND_RANDOM | GRND_NOBLOCK) svc #0 // getrandom for sizes < 256 return 1 in r0, as long as it isn't // interrupted, blocked, or out of entropy, corner cases I don't care // about. :P // fd = stdout // movs r0, #1 // buf = sp mov r1, sp // len = 1 movs r2, #1 // write = 4 movs r7, #4 // load the random byte returned by getrandom() // note: ldrb r5, [sp] is a wide instruction. :( ldrb r5, [r1] // modulo 100 using subtraction loop // x %= 100 .Lmodloop: subs r5, #100 bhi .Lmodloop .Lmodloop.end: // note: this sets the zero flag which is preserved in syscalls adds r5, #100 // store the random byte mod 100 back to [r1] strb r5, [r1] // write(1, &x, 1); svc #0 // syscalls preserve flags, so loop if // the value was zero. bne .Lloop .Lloop.end: // exit movs r7, #1 // exit(dontcare) svc #0  Not gonna win any golfing contests, but I'll post it anyways cuz it is pretty well golfed for what it is. No libc, only uses the getrandom, write, and exit syscalls. Prints raw bytes to stdout, hence why the demo is piped to xxd. Try it online! (sorta) (Travis demo) # Bash, 46 45 bytes until(echo$((RANDOM%100));exit $_);do :;done  Try it online! -1 byte thanks to EasyasPi • You don't need a space after until. Jan 31, 2021 at 7:08 • @EasyasPi Updated Jan 31, 2021 at 7:15 # Wolfram Language (Mathematica), 54 bytes NestWhile[Append[#,Echo@RandomInteger@99]&,{},FreeQ@0]  Try it online! It basically does the same thing as att's 30 byte solution, but additionally it returns the collection of random values as a list, rather than Null • RandomInteger[99] can be replaced with RandomInteger@99 to save one byte. Jan 31, 2021 at 5:51 • @numbermaniac Thanks for pointing that out. I also use FreeQ@0 to replace !MemberQ[#,0]& to save more bytes. Now it looks more compact and readable! Jan 31, 2021 at 5:57 • It is encouraged to keep different language submissions in different answers. Jan 31, 2021 at 6:36 • @Razetime Didn't realize that one can post more than one answer. I'll move the bash solution to it's own answer. Jan 31, 2021 at 6:38 • No problem. Welcome to Code Golf! Jan 31, 2021 at 10:58 # Red, 38 36 bytes until[print p:(random 100)- 1 0 = p]  Try it online! # PHP, 373635 33 bytes for(;$n??1;)echo$n=rand(0,99),~_;  Try it online! Another great way to save 2 bytes by Sisyphus with the PHP 7 ?? operator, and no more preceding space! # PHP, 35 bytes for(;(print~_.$n=rand(0,99))&&$n;);  Try it online! No answer yet for the language we love to hate? Having to display the last zero is not optimal, still trying to get rid of it.. EDIT: finally got rid of the extra display for the zero, with a preceding space # PHP, 36 bytes a:echo~_.$n=rand(0,99);if($n)goto a;  Try it online! A 36 bytes version with an infamous goto, also with a preceding space • That goto is disgusting. I like it. Jan 29, 2021 at 12:49 • Nice one! Another golf still at 35 bytes though! Jan 29, 2021 at 17:02 • @640KB yeah I was wondering if something like my old basic's REPEAT ... UNTIL existed, it does but i thought "too long". My plan was to use &$n with a single & for 34 bytes but for a reason I cannot explain the last zero doesn't show.. Jan 29, 2021 at 20:05
• 33 bytes Jan 30, 2021 at 5:27

# Befunge-98, 19 13 bytes

-3 bytes thanks to @ovs!

?+<1
<@w.:%d'


Try it online!

Generates each number according to a geometric distribution modulo 100.

## Explanation

The instruction pointer (IP) starts at the ? in the top left corner, which sends it in a random direction. If it goes right, the < turns it around back to the ?, and the + (which is executed twice, once in each direction) adds 0 to the current number. If it goes left, 1 puts a 1 on the stack, < sends the IP back to ? again, and + adds the 1 to the number (which, given an empty stack, starts at zero). As long as ? keeps sending the IP left, this can happen arbitrarily many times, so there is a nonzero chance of reaching each number.

If the IP goes up or down, < turns it onto the second line, which processes the number.

 @w.:%d'

:       Copy
.             and print
%                     the number modulo
d'                                     100.
w         If the copy is zero,
@                               end the program.



If the printed result was greater than zero, w sends the IP up to the first line, and < starts the program over from the beginning.

• If you take the random number modulo 100 instead of skipping the print if you reach a number higher than 99, this gets a bit simpler: 16 bytes
– ovs
Feb 2, 2021 at 8:47