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

Mathematica, 28 chars

Sort@RandomInteger[2^32, 100]

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

孤傲高冷的网名
3楼-- · 2019-03-09 02:53

Clojure

(defn gen-rands []
(sort (take 100 (repeatedly #(rand-int Integer/MAX_VALUE)))))
The star\"
4楼-- · 2019-03-09 02:54

APL

13 chars:

a[⍋a←100?9e8]
【Aperson】
5楼-- · 2019-03-09 02:54

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++]));
}
查看更多
萌系小妹纸
6楼-- · 2019-03-09 02:55

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);
    }
}
查看更多
倾城 Initia
7楼-- · 2019-03-09 02:56

Haskell:

import Random
import List
main=newStdGen>>=print.sort.(take 100).randomRs(0,2^32)
登录 后发表回答