The problem is very simple: write a program that accepts a single unsigned integer from the command line, and prints out that many random numbers on the standard output, in ascending order.

For example:



output (you can use any type of whitespace):


However, you are not allowed to define any variables. Any at all.

Score is given by code length, shortest wins, and by up-votes if equal.

The random numbers can be anything generated by a reasonable pseudo-random generator with a uniform distribution: for example, integers at least 8 bits long (no cheap "0 0 0 0 0 1 1 1 1 " one bit solutions, please), or real numbers (e.g. float) between 0 and 1. In dynamically typed languages, the fist assignment counts as definition in our context, so no cheap Matlab solutions, please :)

  • \$\begingroup\$ Is it allowed to use library functions for generating the random numbers? \$\endgroup\$ – paldepind Jun 29 '12 at 15:33
  • \$\begingroup\$ Yes, it is. Just be careful where you store the results. \$\endgroup\$ – vsz Jun 29 '12 at 15:34
  • 2
    \$\begingroup\$ I can hardly refrain from posting the 6 chars GolfScript solution: ~[4]*p. \$\endgroup\$ – Howard Jun 29 '12 at 16:25
  • 15
    \$\begingroup\$ ::doffs moderator hat:: To my mind this relies on a pointless distinction without a difference in which some bits of storage are dubbed "variables" and others are dubbed "not variables". You're fooling yourself if you think that anything interesting is happening here. See another equally silly example in codegolf.stackexchange.com/questions/2027/… . \$\endgroup\$ – dmckee --- ex-moderator kitten Jun 29 '12 at 17:17
  • 1
    \$\begingroup\$ are we allowed to use predefined variables (variables built into the language itself)? \$\endgroup\$ – ardnew Jun 29 '12 at 18:32

14 Answers 14


J, 8 characters

If we can take the number of numbers to be generated at the end of the program like the Q solution, then:



18 21 91 201 212 226 246 253

If not, then taking input from the keyboard requires another 8 characters:


Q, 10

{asc x?1f}


q){asc x?1f}5
`s#0.08724017 0.1024432 0.2310108 0.2560658 0.8671096
  • \$\begingroup\$ Oh, I should have expected this. I should have placed a restriction so that such an easy language (for the problem) should not be allowed. Anyway, congratulations, I think you won! (I will wait a few days with accepting the answer, so that others can gather reputation too, with their painfully clever C++, Javascript and similar solutions) \$\endgroup\$ – vsz Jun 29 '12 at 15:38
  • \$\begingroup\$ Do implicitly defined variables count?! \$\endgroup\$ – skeevey Jun 29 '12 at 16:15

Perl - 28 26 20 chars

say sort map rand.$/,1..<>

I realized the sorting isn't unnecessary, we only need random numbers in ascending order. rand will return a real in [0,1), so just add our iteration counter to each rand result.

say$_+rand for 1..<>



GolfScript, 14 chars


Generating random numbers in GolfScript takes quite many characters, especially if you want to pick them from a reasonably large range. Sorting the list takes only one char ($), though, and needs no variables.

Same code de-golfed and with comments added:

~            # eval input string, turning it into a number
{            # define a code block:
  9.? rand   #   push a random number between 0 and 99-1 onto the stack
} *          # execute the previous code block as many times as the input number indicates
]            # collect the random numbers off the stack into an array
$            # sort the array
`            # un-eval the array for output

Ruby, 42 34

puts (1..gets.to_i).map{rand}.sort

This could probably be made smaller, but don't blame me - I don't even know Ruby. Also, that whitespace after puts is significant. Silly language ;)

  • 1
    \$\begingroup\$ I think you can get rid of the .join bit and use p instead of puts to save around 11 characters. \$\endgroup\$ – Gareth Jul 6 '12 at 7:55
  • \$\begingroup\$ @Gareth: Thanks! I can't use both, though, because p prints it in a list representation. \$\endgroup\$ – Ry- Jul 6 '12 at 13:17

Bash, 45 characters

eval `yes 'echo $RANDOM;'|head -n$1`|sort -n

Put in a file and run sh file.sh 25 or replace $1 by any number


Octave / MATLAB, 23


C, 108 chars

You wanted a C (or derived) answer, so here's one.
Gets the number as the first parameter.
Works successfully with numbers up to 10 or so. With larger numbers, it usually overflows the stack (due to my awful algorithm).


R, 19/43(TRUE random) characters


Just for fun, selecting TRUE random numbers:


Scala, 44:

(1 to readInt)map(util.Random.nextInt)sorted

Python, 73 characters

import sys,random
print sorted(eval("random.random(),"*int(sys.argv[1])))

Hopefully imports don't count as defining variables.

  • \$\begingroup\$ random and sys do count as variables unfortunately (<class: module>) \$\endgroup\$ – MilkyWay90 Nov 25 '18 at 1:42

Haskell (102 / 71 chars)

As a proper program (102 chars)

import Random
import List
main=getLine>>=(\n->getStdGen>>=(print.sort.take n.randomRs(0,256*n))).read

Or in GHCi (71 chars)

:m Random List

JavaScript, 85 bytes

n=>{o=[];for(i=[];i.length<n;i.push(1))o.push(Math.random());return o.sort().join` `}

In JavaScript, arrays are objects, not variables.


Java 8, 96 bytes

Because Java is obviously the right tool for this ! [/sarcasm]


This is a Consumer<Long> or anything that can be implicitly cast to long. Prints random doubles.

n->                                 // Given an input n
   ...Stream.generate(Math::random) // Generate an infinite stream by calling Math.random()
   .limit(n)                        // Limit the stream to n elements
   .sorted()                        // On the sorted stream
   .forEach(System.out::println)    // Print each element on its own line

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