If you think that $filtered is returning a crazy value, add a debug display line to see what the filter command is seeing. i.e. just above the

filter -ff %randq.file %randq.tempfile %randqs.text

... line add a line like:

echo -s $nopath($script) $scriptline filter -ff %randq.file %randq.tempfile %randqs.text

I assume your $mircdir doesn't have any spaces within the path, as that would mess up your script without using quotes when referencing your filename. You might also wish to filter into a @custom window instead of a tempfile, since that makes it a whole lot easier to see what's going in in your temp file.