views:

3955

answers:

63

What is the least amount of code you can write to create, sort (ascending), and print a list of 100 random positive integers? By least amount of code I mean characters contained in the entire source file, so get to minifying.

I'm interested in seeing the answers using any and all programming languages. Let's try to keep one answer per language, edit the previous to correct or simplify. If you can't edit, comment?

+1  A: 

C#:

List<int> TheList = new List<int>();
Random r = new Random();
for ( int i = 0; i < 10; i++ )
    TheList.Add(r.Next());
TheList.Sort();
foreach ( int i in TheList )
    Console.WriteLine(i);

If you're going for the raw character count you might be able to compress this a bit more. But basically this is it.

Edit: Attempt 2:

Random r= new Random();
for ( int i = 0, j=0; i < 100; i++ )
    Console.WriteLine(j+=r.Next(int.MaxValue/100));
Vilx-
this c# answer can be FAR shorter. I'm on iPhone but will edit later
TheSoftwareJedi
Clever way of bypassing the sort by making sure the next number is greater than the last...
BenAlabaster
see linqish version we've created above.
TheSoftwareJedi
The attempt 2 doesn't generate the same distribution of lists of random numbers. For example, one list your code isn't able to generate is [0,1,2,...,99,int.MaxValue] and this is a valid random list.
fortran
What? Did someone say something about distribution?
Vilx-
+8  A: 

My entry:

echo enter a bunch of ints, hit control-D when done
cat - | sort -n

or, per Adam in the comments:

echo enter a bunch of ints, hit control-D when done
sort -n
Paul Tomblin
Hahahah, I love it. =)
BenAlabaster
This is excellent, but should we be counting the code that makes up the shell?
Albert
You don't even need the "cat -" part: just let sort read from stdin!
Adam Rosenfield
@Albert - do the other entries then have to count the code that makes up the run time libraries and the development environment? ;-)
Paul Tomblin
@Albert: if you're going to include the code that makes up the shell, then do you include the C runtime that it's built on? Do you include the OS that that's built on? Do you include the machine's microcoded instructions? Where does the madness end?
Adam Rosenfield
+1 since this actually beats J if you get rid of the echo. I didn't think that was possible.
Meredith L. Patterson
+1  A: 

Python (interactive):

import random
[int(9*random.random())]

What? It creates a list of one random integer, sorts it (trivially), and prints it out.

Ok, here's a serious answer

import random
sorted([int(9*random.random()) for x in range(9)])

It generates 9 random integers in [0, 9), sorts them, and prints them (in an interactive shell).

And here's a shorter variant that actually produces the 100 required:

from random import*
sorted(randint(0,9)for x in' '*100)
Adam Rosenfield
Save 7 bytes:sorted([random.randint(0,9))for x in" "*9])
recursive
recursive, it's a community wiki, you can edit posts yourself.
badp
Recursive, I edited in your solution for you (and saved 3 characters off that). It works in Python 2.6, but can somebody test it in Python3? I don't know if Python 3 lets you go without the []
Wallacoloo
A: 

prints random 100 random numbers in the range [0,100] sorted in C++

srand((unsigned int)time(NULL)); list<int> r;
for (int i=0;i<100;i++) r.push_back((int)((100)*rand()/(float)RAND_MAX));
r.sort();
for (list<int>::iterator j=r.begin();j!=r.end();j++) cout << *j << endl;

If you don't care about parity then replace r.push_back((int)((100)*rand()/(float)RAND_MAX)) with r.push_back(rand()%(101))

--

Here's a complete program in 200 characters:

#include <algorithm>
#include <iostream>
#include <random>
using namespace std;int main(){int a[100];generate_n(a,100,tr1::mt19937());sort(a,a+100);for(int i=0;i<100;++i){cout<<a[i]<<endl;}return 0;}

Array notation was shorter than any standard container I could find. tr1::mt19937 was the shortest random number generator I could find. using namespace std; was shorter than several instances of std::.

mepcotterell
you can use a set instead of a list for sorting, and output iterator for the output.
Raz
+17  A: 

Linux, command line:

% od -dAn -N40 /dev/random | tr ' ' '\n' | sort -nu
4959
6754
8133
10985
11121
14413
17335
20754
21317
30008
30381
33494
34935
41210
41417
43054
48254
51279
54055
55306
adl
Note that the line 'od -dAn -N200 /dev/urandom -w2|sort -n' is shorter, but the output is right aligned with the longest number.
gnud
Try man /dev/random on MacOS X, much better than cat /dev/random. ;)
yogman
Discouraged from here: cat /dev/random actually consume hell lot of time to produce output because it insists on entropy to be gathered by the system. It will usually "freeze" until you type things or move the mouse around to keep producing numbers.
sylvainulg
A: 

Common Lisp (as I remember it, might have a few details wrong):

(setf a '(99 61 47))
(setf a (sort a))
(princ a)

The numbers are of course random; I chose them right now by dice roll.

(Maybe we could tighten up the definition a little?)

David Thornley
Why not `(princ (sort '(99 61 47) #'<))` ? No need to get imperative. Also, you forgot the predicate for sort.
Svante
30 characters with my modification, by the way.
Svante
+3  A: 

Python to print 100 random, sorted integers

import random,sys
print sorted(random.randint(1,sys.maxint)for x in range(100))

@Adam already beat me to it, but I thought using randint() and sys.maxint was sufficiently different to post anyway.

Jay
I was going for brevity, but your post is constructive nonetheless.
Adam Rosenfield
nevermind the fact that your output is not what was requestedimport sys, random print '\n'.join([str(y) for y in sorted([random.randint(1,sys.maxint) for x in range(100)])])
mjard
that went well :\
mjard
well, just depends on whether we're assuming it's supposed to be one per line; description just says "print 100 integers" :-)
Jay
+1  A: 
Byron Whitlock
A: 

Perl command line:

perl -e 'print $_, "\n" foreach sort map {int rand 10} (1..10)'

Prints 10 integers between 0 and 9 and sorts them.

nrich
The sort for numerics needssort { $a <=> $b }Your code only works for single digit numbers, which will sort properly because they are that way in the ASCII table.
xcramps
You can save a few characters with `print "$_\n"`
Platinum Azure
+2  A: 

Perl, a full 8 bytes shorter than nrich's version, and runs under "use warnings;" :)

perl -wle "$,=' ';print sort map {int rand 100} 1..100"
Ant P.
perl -wle "$,=' ';print sort map int rand 100, 1..100" is shorter...
derobert
Yep, you're right. I didn't know a comma would work there.
Ant P.
perl -wE"$,=' ';say sort map int rand 9, 1..100 is even shorter
J.F. Sebastian
Well but all yours code sorts wrong ;-)
Hynek -Pichi- Vychodil
A: 

Java:

  public void randList()
  {
  final int NUM_INTS = 100;
  Random r = new Random();
  List<Integer> s = new ArrayList<Integer>();

  for(int i = 0; i < NUM_INTS; i++)
    s.add(r.nextInt());

  Collections.sort(s);

  for(Integer i : s)
    System.out.println(i);
  }

Could be shorter, but the above is pretty clear and mostly-best-practices-compliant. This will generate warnings (raw types) and has bad variable names but is a little shorter.

  void rl()
  {
  Random r = new Random();
  List s = new ArrayList();

  for(int i = 0; i < 100; i++)
    s.add(r.nextInt());

  for(Object i : s)
    System.out.println((Integer) i);
  }
Adam Jaskiewicz
It's doing something funky to my code...
Adam Jaskiewicz
PhiLho
Thank you! I would have caught that, except that it looked fine in the preview.
Adam Jaskiewicz
A: 

I wouldn't accept that as program spec, too imprecise! :-)

Lua version:

t=table;l={}for i=1,20 do l[#l+1]=math.random(99)end;t.sort(l)print(t.concat(l,' '))

Might be slightly shortened, there might be smarter code too.

PhiLho
+5  A: 

Common Lisp, int between 0 and 10000 (there is no upper bound for that, but you have to choose one).

(sort (loop repeat 100 collect (random 10000)) #'<)
Pål GD
Make it (random 9) for the code golf :)
Svante
It's 47 characters with (random 9), by the way
Svante
You can save 3 chars by removing spaces next to parens!
Ken
+2  A: 

Java:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

class Rnd {
    public static void main(String[] args) {
     List<Integer> list = new ArrayList<Integer>(100);
     for (int i = 0; i < 100; i++) list.add(new Random().nextInt());
     Collections.sort(list);
     System.out.println(list);
    }
}
Pål GD
I like this better than mine as it's a whole application (I cheated by just providing a method) and the meat of it is still more compact.
Adam Jaskiewicz
Does System.out.println(list); actually print out individual Integers? wow. I didn't know that.
Rolf
ArrayList's toString prints the entire list, yes.
Pål GD
Yeah, it does. I didn't use it in mine because IIRC it prints out like [1, 2, 3, ..., n], and I wanted a simple one-integer-per-line output like many other people were doing.
Adam Jaskiewicz
doesn't calling new Random() like that result in the same seed being used over and over within the same millisecond?
TheSoftwareJedi
I think so. Easily solved by running a few instances of Eclipse, though.
Adam Jaskiewicz
+4  A: 

In BASH:

for i in `seq 100`; do echo $RANDOM; done | sort -n
Pål GD
`<powershell user>` And you call *that* elegant or concise? `</powershell user>`
Joey
A: 

A command line PHP one-liner (yes, the trailing semicolon is required...)

php -r 'while (++$i % 101) $j[] = rand(0, 99); sort($j); echo implode(" ", $j)."\n";'
Ant P.
Why not print_r($j)?
Jasper Bekkers
+8  A: 

C#

using System;
using System.Linq;
class A {
    static void Main() {
        var r=new Random();
        new A[100].Select(i=>r.Next()).OrderBy(i=>i).ToList().ForEach(Console.WriteLine);
    }
}

EDIT: made complete program. assumes newlines and spaces could be removed, but left in for clarity :)

EDIT: made even shorter.... I dare someone to improve this one... I've tried for an hour.

EDIT: I think that's a bit shorter.

EDIT: I think that's even more shorter. Ugh, make me stop.

EDIT: One more line, one less character. Debatable...


Explanation

A[100] - an array of any old thing - in this case A's (it's a nice short name). The contents are completely ignored, it's the size of the array that counts.

.Select(i=>r.Next()) - generates an enumerable of 100 values of r.Next().

.OrderBy(i=>i) - sorts the previous in order.

.ToList() - convert the sorted enumerable of int to a List, so we can use ForEach.

ForEach(Console.WriteLine) - call Console.WriteLine 100 times, passing in each integer value in the list.

mackenir
I beat you by 4 characters (even after removing your superfluous System namespace refs:(new int[1000]).Select(i => r.Next()).OrderBy(i => i).ToList().ForEach(Console.WriteLine);
BenAlabaster
And I knocked 2 more chars off changing int to A
TheSoftwareJedi
If only MS had implemented the ForEach extension method on IEnumerable<T>...
mackenir
...and a Sort extension method that was equivalent to OrderBy(i=>i).
mackenir
@mackenir yeah, that ForEach not being on IEnumerable has puzzled me for a while.
TheSoftwareJedi
Damn, good job man. I don't think I can beat that.
BenAlabaster
Blah, I knew I should have learned LINQ. :P
Vilx-
@Vilx since learning the linq lambda stuff - I'm hooked. I use it all over the place. Abusing the ToLIst()ForEach() is a nasty hack though. A foreach over the enumerable is far cleaner.
TheSoftwareJedi
ForEach kind of doesn't belong on the IEnumerable monad (Haskell / theoretical stuff), in the same sense that SelectMany and GroupBy do belong.
Justice
Accidentally deleted comment: see balabaster's answer for shorter.
mackenir
+1  A: 
mzscheme -e "(sort (build-list 100 (λ x (random 9))) <)"

He said the least chars, not the least bytes. =)

Henk
+1  A: 

C#

If you're okay with imposing a limit on the array size then:

Array.ForEach(Guid.NewGuid().ToByteArray().OrderBy(c => c).ToArray(), c => Console.WriteLine(c));

Otherwise, a less restrictive (but slightly more verbose) angle could be taken:

var r = new Random();
(new int[100]).Select(i => r.Next()).OrderBy(i => i).ToList().ForEach(Console.WriteLine);

Okay, I think this is the last time I'm coming back to this one...

116 chars:

using System;
class A
{
    static void Main()
    {
        var r=new Random();
        var n=1D;
        for(int i=0;i<100;i++,Console.WriteLine(n+=r.Next()));
    }
}
BenAlabaster
edit your previous instead of adding more....
TheSoftwareJedi
see linqish version above. it's shorter.
TheSoftwareJedi
I beat you by 4 characters - even after I removed your extra references to the System namespace ;) Beat that sucka :P
BenAlabaster
No, we've still got you beat by 3 chars - assuming you add back in the NEEDED System namespace.
TheSoftwareJedi
Hahaha! I'll get you next time, Gadget!
BenAlabaster
Okay, I'm just going to mumble about how the fact that this outputs doubles somehow makes it wrong... :)
mackenir
lol - hey, nobody specified that doubles weren't allowed :P
BenAlabaster
+45  A: 

10 characters in J:

/:~100?9e9

explanation:

/:~ sorts an array (technically, applies a lists sorted permutation vector to itself)

x ? limit returns x random numbers less than limit

9e9 (9000000000) is a reasonable upper limit expressible in 3 characters. !9 (9 factorial) is smaller, but requires one less character.

Jimmy
thats hot. you win. wtf is J
TheSoftwareJedi
it's one of the APL-family languages. Guaranteed to win code-golf tournaments up until someone enters an entry in Golf. APL is probably slightly shorter than J, but the untypeable character set throws me off. http://en.wikipedia.org/wiki/J_(programming_language)
Jimmy
A: 

VB - 151 chars:

Not quite as elegant as C# sadly...

Sub Main()
    Dim r = New Random
    Enumerable.Range(1, 100) _
      .Select(Function(i) r.Next) _
      .OrderBy(Function(i) i) _
      .ToList _
      .ForEach(AddressOf Console.WriteLine)
End Sub
BenAlabaster
I'm not a VB.NET programmer (just VB6 occasionally).Could you lose p and just do addressof Console.writeline?
mackenir
It would seem that way - good catch :)
BenAlabaster
+5  A: 

An attempt in ruby:

p [].tap{|a|100.times{a<<rand(9e9)}}.sort

(With eight fewer characters, but requiring the tap kestrel of Ruby 1.9)

-for ruby 1.8:

p (0..?d).map{rand 1<<32}.sort

30 characters. (could trim by 2 by changing back to 9e9, but comment in question says range should be MaxInt32.

georg
+2  A: 

groovy:

r=new Random()
List l=[]
100.times{ l << r.nextInt(1000) }
l.sort().each { println it }
Ray Tayek
A: 

This is not a joke. It's the best I can do at the moment. :)

JavaScript:

a=[];for(i=0;i<100;i++){b=Math.round(Math.random()*100);a[i]=b;}c=0;
while(c==0){c=1;for(j=0;j<99;j++){if(a[j]>a[j+1]){d=a[j];a[j]=a[j+1];a[j+1]=d;c=0;}}}
for(k=0;k<100;k++)document.write(a[k],"<br>")
var n=[];while (n.length < 100) n.push(Math.random()*(1<<30)<<0); document.write(n.sort(function(a,b){return a<b?-1:a>b?1:0}).join("<br/>"));
some
var i=0,n=[];for(;i<100;)n[i++]=Math.random()*(1<<30)<<0;document.write(n.sort(function(a,b){return a<b?-1:a>b?1:0}).join("<br/>"));
some
for(i=100,n=[];i;)n[--i]=(1+Math.random())*1e9<<0;alert(n.sort()) // If the alertbox is too wide, use document.write instead
some
<<0 for parseInt is a nice touch :)
Jimmy
It is, isn't it? :)
some
for(i=100,n=[];i;)n[--i]=(1+Math.random())*1e9|0;alert(n.sort())
some
+2  A: 
RobH
ah, apl. how i havent missed you at all.
TrevorD
I've often heard APL being described as a write-only language. :-) With its different set of characters, it's easy to see why, especially when confronted with a complex one-liner such as the one for Conway's game of life shown here (http://catpad.net/michael/apl/).
RobH
Apparently the up arrow HTML 4.0 entities don't work :(
brian d foy
This is not giving a sorted list in my APL interpreter.
fmartin
A: 

Here's the best I can do with a Delphi 2007 Win32 console app (aside from removing some line breaks and indents):

{$APPTYPE CONSOLE}
var a:array[0..99] of integer; i,j,k:integer;
begin
  FOR i:=0 to 99 DO a[i]:=random(maxint)+1;
  FOR i:=0 to 98 DO
    FOR j:=i+1 to 99 DO
      IF a[j]<a[i] THEN begin
        k:=a[i]; a[i]:=a[j]; a[j]:=k
      end;
  FOR i:=0 to 99 DO writeln(a[i])
end.

AFAIK, none of the standard units contain a sorting routine, so I had to write the shortest one I know. Also, since I haven't called Randomize, it produces the same result every time it's run.

Edit: I took "positive integers" to mean every positive (non-zero) number in the range of the integer type, hence the use of maxint (a system constant) and "+1" to ensure it's not zero.

Scott Leis
+3  A: 

Javascript: (via JSDB or Mozilla's Rhino used in shell mode)

x=[];for(i=0;i<100;i++){x.push((Math.random()+"").slice(-8));};x.sort();

Here's a full test run:

c:\>java org.mozilla.javascript.tools.shell.Main
Rhino 1.7 release 1 2008 03 06
js> x=[];for(i=0;i<100;i++){x.push((Math.random()+"").slice(-8));};x.sort();
01499626,02403545,02800791,03320788,05748566,07789074,08998522,09040705,09115996,09379424,10940262,11743066,13806434,14113139,14336231,14382956,15581655,16573104,20043435,21234726,21473566,22078813,22378284,22884394,24241003,25108788,25257883,26286262,28212011,29596596,32566749,33329346,33655759,34344559,34666071,35159796,35310143,37233867,37490513,37685305,37845078,38525696,38589046,40538689,41813718,43116428,43658007,43790468,43791145,43809742,44984312,45115129,47283875,47415222,47434661,54777726,55394134,55798732,55969764,56654976,58329996,59079425,59841404,60161896,60185483,60747905,63075065,69348186,69376617,69680882,70145733,70347987,72551703,73122949,73507129,73609605,73979604,75183751,82218859,83285119,85332552,85570024,85968046,86236137,86700519,86974075,87232105,87839338,88577428,90559652,90587374,90916279,90934951,94311632,94422663,94788023,96394742,97573323,98403455,99465016

edit: looks like I can shorten it a few chars by direct assignment rather than "push", and I don't need the {}s:

x=[];for(i=0;i<100;i++)x[i]=(Math.random()+"").slice(-8);x.sort();
Jason S
x=[];for(i=0;i<100;)x[i++]=(Math.random()+"").slice(-8);x.sort();
some
good one! saved one more character :)
Jason S
for(x=[],i=100;i;)x[--i]=(Math.random()+"").slice(-8);x.sort()
some
Saved some more :)
some
for(i=100,n=[];i;)n[--i]=(1+Math.random())*1e9<<0;n.sort()
some
Trimmed one more: for(i=100,n=[];i;)n[--i]=(1+Math.random())*1e9|0;n.sort()
some
why the "1+"? This looks fine to me: for(i=100,n=[];i;)n[--i]=Math.random()*1e9|0;n.sort();
Luke Schafer
A: 

Plain old C, not so minimal as to be obfuscated:

#include <stdio.h>
#include <stdlib.h>
static int cmp(const void *a, const void *b)
{
    return *(const int *) a > *(const int *) b;
}
int main(void)
{
    int x[100], i;
    for(i = 0; i < 100; i++)
            x[i] = rand();
    qsort(x, 100, sizeof *x, cmp);
    for(i = 0; i < 100; i++)
            printf("%d\n", x[i]);
    return 0;
}

This builds without warnings using gcc 4.1.2 in Linux. In real code, I would of course:

  • Return EXIT_SUCCESS rather than 0
  • Only write the 100 once, and use sizeof x for the remaining references
unwind
+2  A: 

Windows BATCH: 160. This adds a leading zero's to the numbers, but otherwise the sorting is a little messed up (because sort sorts by characters - it doesn't know anything about numbers).

@echo off
set n=%random%.tmp
call :a >%n%
type %n%|sort
del /Q %n%
exit /B 0
:a
for /L %%i in (1,1,100) do call :b
exit /B 0
:b
set i=00000%random%
echo %i:~-5%

As a one-liner and way shorter (72):

cmd/v/c"for /l %x in (0,1,99)do @(set x=0000!RANDOM!&echo !x:~-5!)"|sort
Paulius Maruška
+5  A: 

F#

let r = new System.Random();;

[ for i in 0..100 -> r.Next()] |> List.sort (fun x y -> x-y);;
A: 

Pike

void main() {
  array a=({});
  while (sizeof(a)<100) a+=({random(1<<30)});
  sort(a);
  foreach (a, int b) write("%d\n",b);
}
some
+1  A: 

C++ is not the right tool for this job, but here goes:

#include <algorithm>
#include <stdio.h>

#define each(x) n=0; while(n<100) x

int main()
{
     int v[100], n;
     srand(time(0));
     each(v[n++]=rand());
     std::sort(v, v+100);
     each(printf("%d\n",v[n++]));
}
csl
A: 

I can't edit or comment, so here's Java v3 (the previous 2 had negative numbers):

import java.util.TreeSet;

class Test {

    public static void main(String[] args) {
        Collection<Double> s = new TreeSet<Double>();
        while (s.size() < 100) s.add(Math.random());
        System.out.println(s);
    }
}
+1  A: 

mackenir: an improvement by 7 characters:

namespace System.Linq {
 class A {
  static void Main() {
   var r = new Random();
   new A[100].Select( i => r.Next() ).OrderBy( i => i ).ToList().ForEach( Console.WriteLine );
  }
 }
}
samael
Hah. Nice namespace trick :)
mackenir
A: 

Erlang, 157 chars

-module (intpr).
-export ([q/0]).
q() ->
 lists:foldl(
 fun(X,A)->io:format("~p,",[X]),A end, 
 n, 
 lists:sort( 
  lists:map(fun(_)->random:uniform(100) end, lists:seq(1,100)) 
 )
 ).

Sure it's not the best erlang solution, basically it creates a list from 1 to 100 (only because I didn't found a better/shorter way to initialize the list) and map every item with a random integer (<100), then the resulting list is sorted. Finally the list is "folded", used just as a way to get through all the items and print them.

cheng81
+2  A: 

Clojure

(defn gen-rands []
(sort (take 100 (repeatedly #(rand-int Integer/MAX_VALUE)))))
Chris Bunch
If you drop take and repeatedly, and replace them with 'for', you can shave 5 chars off. Without extraneous whitespace and a function def, you can get down to 54 chars. Even less if you hard code a max-value, like most others have done.
nilamo
(sort(for[x(range 100)][(rand-int Integer/MAX_VALUE)])) Something like that.
nilamo
+19  A: 

xkcd style in PHP:

for($i=0;$i<100;$i++) echo "4\n";
TenebrousX
+1 for xkcd code
jb
Take another +1 for xkcd reference
Platinum Azure
A: 

You would think SQL would be good at this sort of thing:-

SELECT * FROM
(SELECT TRUNC(dbms_random.value(1,100)) r
FROM user_objects
WHERE rownum < 101)
ORDER BY r

That's an Oracle version in 108 characters. Someone might do better in a different variant using a TOP 100 syntax.

Edit, in 82 characters:

select trunc(dbms_random.value(1,100))
from dual 
connect by level < 101
order by 1
WW
+1  A: 

C++ with boost. Too bad that #include's are already half of all the text :)

#include <boost/bind.hpp>
#include <algorithm>
#include <vector>
#include <iterator>
#include <cstdlib>
int main() {
    using namespace std;
    vector<int> a(100);
    transform(a.begin(), a.end(), a.begin(), boost::bind(&rand));
    sort(a.begin(), a.end());
    copy(a.begin(), a.end(), ostream_iterator<int>(cout, "\n"));
}
Johannes Schaub - litb
A: 

Delphi... alphabetically sorted version

program PrintRandomSorted;

{$APPTYPE CONSOLE}
uses SysUtils,Classes;

var I:Byte;
begin
  with TStringList.Create do
  begin
    for I in [0..99] do
      Add(IntToStr(Random(MaxInt)));
    Sort; Write(Text); Free;
  end;
end.


Properly sorted, using generics..

program PrintRandomSorted;
{$APPTYPE CONSOLE}
uses SysUtils, generics.collections;
var I:Byte;S:String;
begin
  with TList<Integer>.Create do
  begin
    for I in [0..99] do Add(Random(MaxInt));
    Sort;
    for I in [0..99] do WriteLn(Items[I]);
    Free;
  end;
end.
Wouter van Nifterick
+7  A: 

Mathematica, 28 chars

Sort@RandomInteger[2^32, 100]

That gives 100 (sorted) random integers in {0,...,2^32}.

dreeves
+1  A: 
#!perl

print join "\n", sort { $a <=> $b } map { int rand 0xFFFFFFFF } 1 .. 100;
brian d foy
A: 

Notice that nobody gave an answer in C or C++ ?

Once they were called high-level languages (compared to assembler). Well I guess now they are the low-level.

Eden
+1  A: 

plain old c-code in 167 chars:

main(){int i=100,x[i],n=i;while(i)x[--i]=rand();for(i=0;i<n;i++){int b=x[i],m=i,j=0;for(;j<n;j++)if(x[j]<x[m])m=j;x[i]=x[m];x[m]=b;}i=n;while(i)printf("%d ",x[--i]);}
Roman Glass
+5  A: 

APL

15 chars (counting the newline):

a←100?9e8
a[⍋a]

The first line assigns 100 different random value from 1 to 9e8 to array a.

The second line prints it sorted.

fmartin
And without any Greek! I'm impressed.
Mark Ransom
+1  A: 

Java, again

import java.util.*;
class R
{
    public static void main(String[]a)
    {
        List x=new Stack();
        while(x.size()<100)x.add((int)(Math.random()*9e9));
        Collections.sort(x);
        System.out.print(x);
    }
}

i don't think it can be made shorter than this.. i also cut out unnecessary spaces.

LE: oh yes it can :) inspired by ding's post..

import java.util.*;
class R
{
    public static void main(String[]a)
    {
        Set x=new TreeSet();
        while(x.size()<100)x.add((int)(Math.random()*9e9));
        System.out.print(x);
    }
}
+4  A: 

Haskell:

import Random
import List
main=newStdGen>>=print.sort.(take 100).randomRs(0,2^32)
hiena
Can be shortened (and in my opinion it is even nicer and easier to read :) ). Modules are the same - main=newStdGen>>=print.sort.take 100.randomRs(0::Int,9)
Matajon
I made it shorter (and changed the upper bound :-p)
jleedev
A: 

python, 71 chars

import random
print sorted(random.randint(0,2**31)for i in range(100))
fortran
+1  A: 

In OCaml:

List.sort compare (let rec r = function 0 -> [] | a -> (Random.int 9999)::(r (a-1)) in r 100);;

Edit: in OCaml typing that in the toplevel will print out the list, but if you want the list printed to stdout:

List.iter (fun x -> Printf.printf "%d\n" x) (List.sort compare (let rec r = function 0 -> [] | a -> (Random.int 9999)::(r (a-1)) in r 100));;
Niki Yoshiuchi
A: 

Coldfusion:

<cfloop index="i" to="100" from="1">
    <cfset a[i] = randrange(1,10000)>
</cfloop>

<cfset ArraySort(a, "numeric")>

<cfdump var="#a#">
dhorn
A: 

Qt4 version (c++), 116 chars.

#include <QtCore>
int main(){QList<int>l;int i=101;while(--i)l<<qrand()%101;qSort(l);foreach(i,l)printf("%d\n",i);}

-> wc -c main.cpp
116 main.cpp
OneOfOne
+3  A: 

Powershell :

35 chars (with PowerShell Community Extensions, which replaces Get-Random):

0..99|%{[int]((random)*10000)}|sort

20 characters (plain PowerShell v2):

0..99|%{random}|sort
Raoul Supercopter
A: 

C#

var sequence = Enumerable.Range(1, 100)
                         .OrderBy(n => n * n * (new Random()).Next());

foreach (var el in sequence.OrderBy(n => n))
    Console.Out.WriteLine(el);

F#

let rnd = System.Random(System.DateTime.Now.Millisecond)

List.init 100 (fun _ -> rnd.Next(100)) 
|> List.sort 
|> List.iter (fun (x: int) -> System.Console.Out.WriteLine(x))
Ray
You can use List.iter (printfn "%d").
Matajon
+1  A: 

Prolog, 78 characters:

r([]).
r([H|T]):-random(0,1000000,H),r(T).
f(L):-length(R,100),r(R),sort(R,L).

usage in the REPL:

| ?- f(X).

X = [1251,4669,8789,8911,14984,23742,56213,57037,63537,91400,92620,108276,119079,142333,147308,151550,165893,166229,168975,174102,193298,205352,209594,225097,235321,266204,272888,275878,297271,301940,303985,345550,350280,352111,361328,364440,375854,377868,385223,392425,425140,445678,450775,457946,462066,468444,479858,484924,491882,504791,513519,517089,519866,531646,539337,563568,571166,572387,584991,587890,599029,601745,607147,607666,608947,611480,657287,663024,677185,691162,699737,710479,726470,726654,734985,743713,744415,746582,751525,779632,783294,802581,802856,808715,822814,837585,840118,843627,858917,862213,875946,895935,918762,925689,949127,955871,988494,989959,996765,999664]

yes

As some of my teachers would say, it's self explanatory :-)

fortran
A: 

C without allocating an array (basically keeps adding a random integer to the last value printed, which implicitly sorts):

#include <stdlib.h>

int main(int argc, char **argv) {
    int i;
    unsigned long baseline = 0;
    srand(atoi(argv[1]));
    for ( i = 0 ; i < 100 ; i++ ) {
     baseline += rand();
     printf("%ld\n", baseline);
    }

}

which, with the usual obfuscation+removal of newlines, goes down to 136 chars.

ps: run with $RANDOM on the command line, obviously.

lorenzog
A: 

C#

var r=new Random();
var l=from i in new C[100] let n=r.Next() orderby n select n;

81 characters (including newlines)

Chris Nash
A: 

Matlab:

arrayfun( @(x) display(x), sort( rand(1,100) ) )
mvaz
A: 

Using C - 152 chars

main(){int a=100,b,t,x[a];while(a--)x[a]=rand();a=100;while(b=a--)while(b--)if(x[a]>x[b]){t=x[a];x[a]=x[b];x[b]=t;}a=100;while(a--)printf("%d\n",x[a]);}

or expanded

#include <stdio.h>
int main()
{
    int a=100,b,t,x[a];

    while(a--)x[a]=rand();
    a=100;

    while(b=a--)
        while(b--)
            if(x[a]>x[b])
            {
                t=x[a];
                x[a]=x[b];
                x[b]=t;
            }

    a=100;
    while(a--)
        printf("%d\n",x[a]);
}
jme
+1  A: 

Golfscript - 15 chars

[100.{rand}+*]$
gnibbler
A: 

R:

sort(round(runif(100)*99+1))

A: 

Windows command shell - 71 characters

cmd/v/c"for /L %i in (0,1,99)do @set r=     !random!&echo !r:~-5!"|sort
Carlos Gutiérrez
+1  A: 

xkcd-style Python answer - shorter than the current one!

print [4]*100

Real answer, 63 chars:

from random import*;print sorted(randrange(9e9)for i in[0]*100)
Claudiu
A: 

Scala: (61 characters)

1 to 100 map {_ ⇒ Random.nextInt} sortBy identity foreach println

EDIT:

Could be compressed even more (56 characters) by defining identity inline.

1 to 100 map {_ ⇒ Random.nextInt} sortBy {i ⇒ i} foreach println
missingfaktor