in reply to Re: How to randomly select a key whose value is between a range?
in thread How to randomly select a key whose value is between a range?

... and notice how golux neatly side-stepped that issue in his (excellent) post #1:   the relevant range of keys was extracted (using grep as a clever alternative to a loop, to iterate through the entire set of key returned by keys), and placed into an array, which of course uses a zero-based numeric index.   The keys were selected from this array.   As long as you determine that the list contains at least one key, this approach always works.

If you need to randomly select more than one key within the range, such that the same key will never be selected twice, a good way to do so is:   select the array of keys (as shown), then shuffle the array, then draw from the top.   Array::Shuffle and Algorithm::Numerical::Shuffle both illustrate how to do this.   (Fair warning:   read the fine-print in delete with regard to arrays, if you’re considering other ways to do this.)