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:

input:

5


output (you can use any type of whitespace):

557
9424
19212
32293
39135


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 :)

## closed as unclear what you're asking by Mego♦, FryAmTheEggman, NoOneIsHere, DJMcMayhem♦, BlueMay 13 '16 at 17:41

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• Is it allowed to use library functions for generating the random numbers? – paldepind Jun 29 '12 at 15:33
• Yes, it is. Just be careful where you store the results. – vsz Jun 29 '12 at 15:34
• I can hardly refrain from posting the 6 chars GolfScript solution: ~[4]*p. – Howard Jun 29 '12 at 16:25
• ::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/… . – dmckee Jun 29 '12 at 17:17
• are we allowed to use predefined variables (variables built into the language itself)? – ardnew Jun 29 '12 at 18:32

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

/:~256?~


Example:

   /:~256?~8
18 21 91 201 212 226 246 253


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

/:~256?~".1!:1[1


# Q, 10

{asc x?1f}


.

q){asc x?1f}5
s#0.08724017 0.1024432 0.2310108 0.2560658 0.8671096

• 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) – vsz Jun 29 '12 at 15:38
• Do implicitly defined variables count?! – 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..<>


or

map{say$_+rand}1..<>  ## GolfScript, 14 chars ~{9.?rand}*]$


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 ;)

• I think you can get rid of the .join bit and use p instead of puts to save around 11 characters. – Gareth Jul 6 '12 at 7:55
• @Gareth: Thanks! I can't use both, though, because p prints it in a list representation. – 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

sort(rand(input(""),1))


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

f(x,m,r){x&&f(x-(r>m),r>m?printf("%d\n",r),r:m,rand());}
main(c,v)char**v;{srand(time(0));f(atoi(v[1]),0,0);}


## R, 19/43(TRUE random) characters

sort(runif(scan()))


Just for fun, selecting TRUE random numbers:

library(random);sort(randomNumbers(scan()))


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

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

# Haskell (102 / 71 chars)

As a proper program (102 chars)

import Random
import List


Or in GHCi (71 chars)

:m Random List
getLine>>=(fmapgetStdGen).(sort.).(.randoms).take.read


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

n->java.util.stream.Stream.generate(Math::random).limit(n).sorted().forEach(System.out::println)


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
`