-6
\$\begingroup\$

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

\$\endgroup\$

closed as unclear what you're asking by Mego, FryAmTheEggman, NoOneIsHere, DJMcMayhem, Blue May 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.

  • \$\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 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

7
\$\begingroup\$

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

Q, 10

{asc x?1f}

.

q){asc x?1f}5
`s#0.08724017 0.1024432 0.2310108 0.2560658 0.8671096
\$\endgroup\$
  • \$\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
2
\$\begingroup\$

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

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

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

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

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

\$\endgroup\$
1
\$\begingroup\$

Octave / MATLAB, 23

sort(rand(input(""),1))
\$\endgroup\$
1
\$\begingroup\$

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);}
\$\endgroup\$
1
\$\begingroup\$

R, 19/43(TRUE random) characters

sort(runif(scan()))

Just for fun, selecting TRUE random numbers:

library(random);sort(randomNumbers(scan()))
\$\endgroup\$
1
\$\begingroup\$

Scala, 44:

(1 to readInt)map(util.Random.nextInt)sorted
\$\endgroup\$
0
\$\begingroup\$

Python, 73 characters

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

Hopefully imports don't count as defining variables.

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

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
getLine>>=(`fmap`getStdGen).(sort.).(.randoms).take.read
\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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

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