39
\$\begingroup\$

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.

\$\endgroup\$
18
  • 1
    \$\begingroup\$ @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 \$\endgroup\$ – caird coinheringaahing Jan 28 at 14:52
  • 2
    \$\begingroup\$ Surely if the integers must be chosen independently, then the output cannot be unique? \$\endgroup\$ – pxeger Jan 28 at 15:50
  • 4
    \$\begingroup\$ @cairdcoinheringaahing for example, if a 5 is chosen, the chance of choosing another 5 has changed to 0. That isn't independent. \$\endgroup\$ – pxeger Jan 28 at 16:06
  • 7
    \$\begingroup\$ @pxeger FWIW I agree with pxeger. The output numbers being unique is not compatible with independence \$\endgroup\$ – Luis Mendo Jan 28 at 16:15
  • 1
    \$\begingroup\$ @LuisMendo Yep, that’s fine \$\endgroup\$ – caird coinheringaahing Jan 28 at 16:40

72 Answers 72

4
\$\begingroup\$

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
   
\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ zsh - or pretty much any other shell. \$\endgroup\$ – Digital Trauma Jan 28 at 16:18
  • 1
    \$\begingroup\$ @DigitalTrauma I just use zsh because I have it bookmarked on TIO :q \$\endgroup\$ – pxeger Jan 28 at 16:20
3
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

Bash, 60 bytes

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

Try it online!

\$\endgroup\$
3
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – GammaFunction Jan 30 at 11:02
  • 2
    \$\begingroup\$ -1 byte: $(($RANDOM%100)) -> $((RANDOM%100)) \$\endgroup\$ – navigaid Jan 31 at 7:29
  • \$\begingroup\$ -9 bytes: for((;;)){ echo $[RANDOM%100];}|grep -B99999 -m1 ^0 .. test it \$\endgroup\$ – roblogic Mar 21 at 8:32
3
\$\begingroup\$

Wolfram Language (Mathematica), 30 bytes

While[Echo@RandomInteger@99>0]

Try it online!

\$\endgroup\$
3
\$\begingroup\$

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

\$\endgroup\$
3
\$\begingroup\$

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
\$\endgroup\$
3
\$\begingroup\$

Julia, 27 bytes

while 0<@show rand(0:99)end

Try it online!

\$\endgroup\$
3
\$\begingroup\$

PowerShell, 45 36 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

\$\endgroup\$
3
  • \$\begingroup\$ 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! \$\endgroup\$ – Zaelin Goodman Jan 29 at 19:13
  • \$\begingroup\$ 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)} \$\endgroup\$ – Zaelin Goodman Jan 29 at 19:20
  • \$\begingroup\$ @ZaelinGoodman thanks for it \$\endgroup\$ – Wasif Jan 30 at 2:17
3
\$\begingroup\$

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)

\$\endgroup\$
1
  • 1
    \$\begingroup\$ -1 byte for /dev/random, although TIO's jail artificially prevents accessing it. \$\endgroup\$ – EasyasPi Jan 30 at 17:30
3
\$\begingroup\$

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>

\$\endgroup\$
8
  • 2
    \$\begingroup\$ Could you edit in a link to an online interpreter such as Tryitonline! so that others can test your program? \$\endgroup\$ – caird coinheringaahing Jan 28 at 16:38
  • \$\begingroup\$ idk how to get jquery working on TIO. \$\endgroup\$ – Alex bries Jan 28 at 16:50
  • \$\begingroup\$ $.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). \$\endgroup\$ – Arnauld Jan 28 at 21:43
  • \$\begingroup\$ Is using $.now() generate random number multiple times allowed on this site? \$\endgroup\$ – tsh Jan 29 at 2:03
  • 1
    \$\begingroup\$ Can it ever generate 99? \$\endgroup\$ – l4m2 Mar 16 at 20:35
3
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

Red, 32 bytes

until[0 = probe -1 + random 100]

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ 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 :) \$\endgroup\$ – caird coinheringaahing Jun 4 at 14:04
2
\$\begingroup\$

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!

\$\endgroup\$
2
\$\begingroup\$

Charcoal, 10 bytes

W‽¹⁰⁰⟦Iι⟧0

Try it online! Link is to verbose version of code. Explanation:

W‽¹⁰⁰

Repeat while a randomly chosen integer from 0 to 99 is non-zero...

⟦Iι⟧

... 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.

\$\endgroup\$
2
\$\begingroup\$

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!

\$\endgroup\$
2
\$\begingroup\$

Scala, 65 bytes

def f=Seq.fill(99999)(util.Random.nextInt(100)).takeWhile(_>0):+0

Try it online!

\$\endgroup\$
2
\$\begingroup\$

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

\$\endgroup\$
1
  • 1
    \$\begingroup\$ x!=0 --> x>0 \$\endgroup\$ – Danis Jan 28 at 20:34
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

Racket, 58 bytes

(define(f[r(random 100)])(and(writeln r)(when(< 0 r)(f))))

Try it online!

\$\endgroup\$
2
\$\begingroup\$

K (oK), 18 16 bytes

(*|){x,1?100}/""

Try it online!

\$\endgroup\$
2
\$\begingroup\$

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!

\$\endgroup\$
2
\$\begingroup\$

AWK, 51 bytes

{for(srand(systime());$++x=int(100*rand()%100););}1

This works by assigning positional variables random numbers until a 0 is generated.

The awfully long,

srand(systime())

line is needed to seed the random number generator. I couldn't find a shorter way to do that without passing in random info from outside the script.

The only other trick here is using a "truthy" test with no action, which trigger AWK to print all the positional variables by default.

Try it online!

\$\endgroup\$
2
\$\begingroup\$

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

\$\endgroup\$
2
\$\begingroup\$

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)

\$\endgroup\$
2
\$\begingroup\$

Bash, 46 45 bytes

until(echo $((RANDOM%100));exit $_);do :;done

Try it online!

-1 byte thanks to EasyasPi

\$\endgroup\$
2
  • 1
    \$\begingroup\$ You don't need a space after until. \$\endgroup\$ – EasyasPi Jan 31 at 7:08
  • \$\begingroup\$ @EasyasPi Updated \$\endgroup\$ – navigaid Jan 31 at 7:15
2
\$\begingroup\$

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

\$\endgroup\$
5
  • 1
    \$\begingroup\$ RandomInteger[99] can be replaced with RandomInteger@99 to save one byte. \$\endgroup\$ – numbermaniac Jan 31 at 5:51
  • \$\begingroup\$ @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! \$\endgroup\$ – navigaid Jan 31 at 5:57
  • \$\begingroup\$ It is encouraged to keep different language submissions in different answers. \$\endgroup\$ – Razetime Jan 31 at 6:36
  • 1
    \$\begingroup\$ @Razetime Didn't realize that one can post more than one answer. I'll move the bash solution to it's own answer. \$\endgroup\$ – navigaid Jan 31 at 6:38
  • \$\begingroup\$ No problem. Welcome to Code Golf! \$\endgroup\$ – Razetime Jan 31 at 10:58
2
\$\begingroup\$

Red, 38 36 bytes

until[print p:(random 100)- 1 0 = p]

Try it online!

\$\endgroup\$
2
\$\begingroup\$

PHP, 37 36 35 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

\$\endgroup\$
4
  • 1
    \$\begingroup\$ That goto is disgusting. I like it. \$\endgroup\$ – EasyasPi Jan 29 at 12:49
  • \$\begingroup\$ Nice one! Another golf still at 35 bytes though! \$\endgroup\$ – 640KB Jan 29 at 17:02
  • \$\begingroup\$ @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.. \$\endgroup\$ – Kaddath Jan 29 at 20:05
  • \$\begingroup\$ 33 bytes \$\endgroup\$ – Sisyphus Jan 30 at 5:27

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.