It was a normal presentation that I were in as a audience, until the presenter gave a math problem about repeat taking 2 number out of a list a replacing them with average, claiming that there will be something special about it, and our math teacher, sitting at the end of classroom, exciting rushed to me, and tasked me to code that out.


2 input length and generation

  1. Generate a range, start from 0, with length of length

  2. Randomly choose 2 number (or item) X and Y and replace both X and Y with the average of X and Y

  3. Repeat step 2 for generation times

  4. Output minimum, maximum, average, and the processed list.


I'll let this semi-golfed python code explain itself :)

import random
for i in range(b):


  • Standard rules applies
  • Input can be in any convenience form.
  • As long as you output is clean, understandable, readable, it is allowed.
  • The two random value X,Y should be uniform statistically independent random by itself(X and Y are not affecting each other no matter what)

what you should do?

  • Start golfing right now!
  • \$\begingroup\$ Technically the procedure doesn't change the average, it's always (length-1)/2, isn't it? \$\endgroup\$
    – Neil
    Dec 23, 2021 at 0:04
  • \$\begingroup\$ @Neil exactly yes, you are smart, you can also prove it with math. \$\endgroup\$
    – okie
    Dec 23, 2021 at 0:07
  • \$\begingroup\$ Can X and Y be equal? \$\endgroup\$ Dec 23, 2021 at 0:14
  • 4
    \$\begingroup\$ "Randomly choose 2 number" - this needs further specification. Does it have to be a uniform random distribution? Must they be independently chosen? \$\endgroup\$
    – pxeger
    Dec 23, 2021 at 1:27
  • 3
    \$\begingroup\$ Is it random with replacement or without? \$\endgroup\$
    – Wheat Wizard
    Dec 23, 2021 at 8:54

12 Answers 12


Jelly, 25 bytes


Try it online!

Outputs [[min, max, mean], list]. Takes length as the first argument, and generation as the second

How it works

ḶJX,XƊṬ×ịÆm¥oʋƊ⁹¡µṂ;Ṁ;Æm, - Main link. Takes L on the left, G on the right
Ḷ                         - [0, 1, ..., L-1]
              Ɗ           - Previous 3 links as a monad f(R):
 J                        -   Indices of R
     Ɗ                    -   To the indices of R:
  X                       -     Randomly choose one
    X                     -     Randomly choose one again
   ,                      -     Pair; [i, j]
             ʋ            -   Last 4 links as a dyad g([i, j], R):
      Ṭ                   -     Boolean array with 1s at i and j
           ¥              -     Last 2 links as a dyad h([i, j], R):
        ị                 -       Get R[i] and R[j]
         Æm               -       Take the mean
       ×                  -     Replace the 1s with the mean
            o             -     Or; replace the elements at i and j in R with the mean
               ⁹¡         - Apply f(R) G times
                 µ        - Use this list L' as the new argument
                  Ṃ       - Minimum
                    Ṁ     - Maximum
                      Æm  - Mean
                   ; ;    - [Min, Max, Mean]
                        , - Pair with L'

APL+WIN, 60 bytes

Prompts for length and generation. Index origin = 0


Try it online!Thanks to Dyalog Classic

The results in TIO are output in the order listed in the question. The second example in TIO shows that by generation=1000 all elements of the output have converged to the mean in that particular case.


Vyxal, 25 bytes


Try it Online!

25 bytes of fun. Takes length then generations and outputs [list, [Max, Min], average]. Can be 24 bytes with the W flag


ʁ                          # The range [0, length) - this will be the list we modify
 ?(                        # (generation) times:
   ₅‹                      #   push the top of the stack, and its length + 1
     ₍℅℅                   #   choose two random items from the range [0, length) and place them into a list
        ~İ                 #   without popping anything, get the item at each randomly chosen index from the top list - this leaves [list, indexes, items] on the stack
          ṁ£               #   place the average of those two items into the register
            (...)          #   for each index i in the indexes list:
             n¥Ȧ           #      list[i] = register (the mean)
                 )         # close the main for loop
                  :        # now the top of the stack is the list with all the replaced items. We duplicate it so we can extract the juicy info from it
                   ₌₍Ggṁ   # Push a list of [max, min] and push the mean of that list
                        W  # Wrap everything into a single list and implicitly print
  • \$\begingroup\$ the length, the mighty lyxal god, you forgot about the length!(the list length your code generated is length+1) \$\endgroup\$
    – okie
    Dec 23, 2021 at 0:13
  • 1
    \$\begingroup\$ @okie fixed‎‎ ‎ ‎ \$\endgroup\$
    – lyxal
    Dec 23, 2021 at 0:15

JavaScript (ES6), 145 bytes

Expects (length)(generation). Returns [min, max, avg, list].


Try it online!

  • \$\begingroup\$ you forgot the statics about the processed list (average,min,max) \$\endgroup\$
    – okie
    Dec 23, 2021 at 0:31
  • \$\begingroup\$ @okie Ugh. I missed that part entirely ... :-/ Edit: now fixed. \$\endgroup\$
    – Arnauld
    Dec 23, 2021 at 0:32
  • \$\begingroup\$ Maybe you can simplify eval(a.join`+`)/n to ~-n/2. \$\endgroup\$
    – tsh
    Dec 23, 2021 at 3:08

Python, 134 bytes

from random import*
def f(l,g):*a,=range(l);exec("a[x]=a[y]=a[x:=R(l)]/2+a[y:=R(l)]/2;"*g);return min(a),max(a),sum(a)/l,a

Attempt This Online!

-13 bytes thanks to pxeger using randrange and :=

  • \$\begingroup\$ your loop method looks awesome! I didn't know you could do that if you don't need iter value \$\endgroup\$
    – okie
    Dec 23, 2021 at 0:46
  • 1
    \$\begingroup\$ @okie Ah, yeah, it's a pretty helpful tip. Anyway, if you don't need the iter value, another thing you can do is just repeat the code and exec it :P \$\endgroup\$
    – hyper-neutrino
    Dec 23, 2021 at 1:18
  • 1
    \$\begingroup\$ 134 bytes \$\endgroup\$
    – pxeger
    Dec 23, 2021 at 1:23

Charcoal, 39 bytes


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


Generate a range of length length.


Generate generation pairs of random integers also in that range, and loop over those pairs.


Update the array at those two indices with the average of the two values that are there.


Output the minimum, maximum, average and values.


Python3, 133 131 128 bytes

from random import*
def f(l,g):
 while g:shuffle(r);*r,x,y=r;r+=[(x+y)/2]*2;g-=1
 return min(r),max(r),sum(r)/l,r

Try this online


J, 44 bytes


Try it online!

Outputs Min / Max / List / Avg


Factor, 87 bytes

[ iota >array [ randomize 2 cut* mean dup 2array append ] repeat dup minmax pick mean ]

Try it online!


BQN (CBQN), 56 bytes


Attempt This Online!

fixed after ovs' comment.

-1 from ovs.

  • 1
    \$\begingroup\$ does that recompute 2•rand.Range𝕩 every iteration? \$\endgroup\$
    – Razetime
    Apr 8, 2022 at 13:19
  • 1
    \$\begingroup\$ No it didn't. But ∾˜∘A saves a byte over (2⥊A) \$\endgroup\$
    – ovs
    Apr 8, 2022 at 13:41

Julia, 81 bytes


Try it online!

expects length\generation and returns ((min, max), mean, list)


TI-Basic, 85 bytes

Prompt L,G
Disp min(ʟA),max(ʟA),mean(ʟA),ʟA

seq(I,I, can be replaced with randIntNoRep( for -3 bytes if the calculator supports it.

Output is displayed.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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