One of the virtues of J is its conciseness; I think this is underrated in exploratory programming. Here I present some J one-liners alongside samples of other languages.

GCD of Random Integers

Problem: pick two random integers between 1 and 1,000,000,000 and find their GCD.

This Python solution is from @MemeArcana:

>>> from random import randint
>>> from math import gcd
>>> gcd(*[randint(1, 1\_000\_000\_000) for \_ in range(2)])
1

Compare to J

   +./ ?2#1000000000
1

(thanks to Randy MacDonald for a correction)

Shuffle a 1-D Array

Python:

>>> import random
>>> random.shuffle(array)

J:

(?~@#{[)

This is characteristic of the APL family: it doesn't depend on a library but it still uses fewer characters!

This can be extended to an array of arbitrary dimension:

($ $ (?~@#{[)@,)

Linear Regression

Python and R from Richard Dinga:

lm(y ~ x)
>>> import numpy as np
>>> from sklearn.linear_model import LinearRegression
>>> reg = LinearRegression().fit(x, y)

J:

y %. 1 ,. x

Sample Without Replacement

To sample x items from a 1-D array y:

x {.?~# y

Generate a Random Permutation and Find its Inverse

C example from the GSL manual:

\#include <stdio.h>
\#include <gsl/gsl\_rng.h>
\#include <gsl/gsl\_randist.h>
\#include <gsl/gsl\_permutation.h>

int
main (void)
{
  const size\_t N = 10;
  const gsl\_rng\_type * T;
  gsl\_rng \* r;

  gsl\_permutation \* p = gsl\_permutation\_alloc (N);
  gsl\_permutation \* q = gsl\_permutation\_alloc (N);

  gsl\_rng\_env\_setup();
  T = gsl\_rng\_default;
  r = gsl\_rng\_alloc (T);

  gsl\_permutation\_init (p);

  gsl\_ran\_shuffle (r, p->data, N, sizeof(size\_t));

  gsl\_permutation\_inverse (q, p);

  gsl\_permutation\_free (p);
  gsl\_permutation\_free (q);
  gsl\_rng\_free (r);

  return 0;
}

In J:

/: ?~ 10