Это алгоритм перетасовки knuth.
public class Knuth {
// this class should not be instantiated
private Knuth() { }
/**
* Rearranges an array of objects in uniformly random order
* (under the assumption that <tt>Math.random()</tt> generates independent
* and uniformly distributed numbers between 0 and 1).
* @param a the array to be shuffled
*/
public static void shuffle(Object[] a) {
int n = a.length;
for (int i = 0; i < n; i++) {
// choose index uniformly in [i, n-1]
int r = i + (int) (Math.random() * (n - i));
Object swap = a[r];
a[r] = a[i];
a[i] = swap;
}
}
/**
* Reads in a sequence of strings from standard input, shuffles
* them, and prints out the results.
*/
public static void main(String[] args) {
// read in the data
String[] a = StdIn.readAllStrings();
// shuffle the array
Knuth.shuffle(a);
// print results.
for (int i = 0; i < a.length; i++)
StdOut.println(a[i]);
}
}
Я думаю, что Вы хотите этот http://pgl.yoyo.org/luai/i/io.popen io.popen. Но это не всегда компилируется в.
Если у Вас есть io.popen, то это - то, что я использую:
function os.capture(cmd, raw) local f = assert(io.popen(cmd, 'r')) local s = assert(f:read('*a')) f:close() if raw then return s end s = string.gsub(s, '^%s+', '') s = string.gsub(s, '%s+, Если у Вас нет io.popen, тогда по-видимому, popen (3), не доступно в Вашей системе, и Вы находитесь в глубоком йогурте. Но все порты Lua Unix/Mac/окон будут иметь io.popen.
, '') s = string.gsub(s, '[\n\r]+', ' ') return s end
, Если у Вас нет io.popen, тогда по-видимому, popen (3), не доступно в Вашей системе, и Вы находитесь в глубоком йогурте. Но все порты Lua Unix/Mac/окон будут иметь io.popen.
Я не знаю о Lua конкретно, но можно обычно выполнять команду как:
comd >comd.txt 2>&1
для получения вывода и ошибки в файл comd.txt затем используйте функции файлового ввода-вывода языков для чтения его в.
Это - то, как я сделал бы это, если бы сам язык не предусмотрел получение stanard вывод и ошибка.