Bulls and cows: Difference between revisions

Line 612:
=={{header|D}}==
<lang d>import std.stdio, std.random, std.string;
 
T[] sample(T)(T[] data, int n) {
auto result = new T[n];
auto pool = data.dup;
foreach (i; 0 .. n) {
int j = uniform(0, data.length - i);
result[i] = pool[j];
pool[j] = pool[data.length - i - 1];
}
return result;
}
 
void main() {
int size = 4;
char[] digits = "123456789".dup;
char[] chosen = sample(digits, size).dup;
randomShuffle(chosen);
chosen = chosen[0 .. size];
 
writefln("Guess a number composed of %d unique digits from" ~
" 1 to 9 in random order.", size, size);
Anonymous user