The following code shows how to randomize a queue. The use of unique
is obvious, but the trick is
in using size
as a constraint, which lets one to control how many elements will be randomized.
int q[$];
...
q.delete();
assert(
std::randomize( q ) with {
unique{q};
q.size == requested_size;
foreach(q[i]) ...element constraint...;
}
);
It may be also handy to sort or randomize the order of queue/array elements. Note, though, that the ordering affects the array itself, rather than returning a new, derived queue/array.
q.sort(); // sort
q.shuffle(); // random order of elements