Create, sort, and print a list of 100 random ints

2019-03-09 02:12发布

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?

30条回答
对你真心纯属浪费
2楼-- · 2019-03-09 02:58

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 );
        }
    }
}
查看更多
走好不送
3楼-- · 2019-03-09 03:00

Tcl is dead.

Long live tcl.

Creates a RANDOM (0-99) length list and puts RANDOM (0-99) integers in it.

Also prints to the screen and can be run exactly as shown in a tcl file, or the tcl shell.

set l {}
proc r {} {expr { int(floor(rand()*99)) }}
for {set i 0} {$i<[r]} {incr i} {lappend l [r]}
puts [lsort -integer $l]

PHP is nice too.

confirms completely to exercise


<?
for($i=100;$i--;$l[]=rand());
sort($l);
print_r($l);

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-03-09 03:01

F#

let r = new System.Random();;

[ for i in 0..100 -> r.Next()] |> List.sort (fun x y -> x-y);;
查看更多
疯言疯语
5楼-- · 2019-03-09 03:02

APL (interactive):

If you want the numbers 0-99 (or 1-100, depending on whether you have the index origin in your workspace set to 0 or 1) to be unique, it takes 8 characters, like so:

↑100?100

If you don't care about uniqueness, do this (9 characters):

↑?100ρ100

Want larger numbers? Just substitute your upper limit, N, for the second 100 on each line, and your random numbers will be in the range 0 - N-1 (or 1-N if your index origin is set to 1).

If you want to guarantee that your numbers range from 0-99 (or 0 - N-1 if you're going for a larger upper limit) regardless of the index origin setting, just enclose either of the above lines in parentheses and add

-⎕IO

to the end (where ⎕ is APL's quad character). That's an additional 6 characters.

查看更多
Root(大扎)
6楼-- · 2019-03-09 03:02

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
查看更多
放荡不羁爱自由
7楼-- · 2019-03-09 03:03

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.

查看更多
登录 后发表回答