Search code examples
randomunique

choose random block of numbers without repetition


I have a list of numbers from 1 to 100. I need to choose a block of numbers of a random size. For example, first time choose number 5 to 21 second time choose number 50 to 66 Continue choosing a block of numbers until all numbers have been chosen, i.e., I cover the whole range (which is 100). The numbers should not be repeated in any block, meaning that the ranges should be unique.

I need a general algorithm not related to a specific programming language.


Solution

    • Slice your list up into random sized blocks (pick a random size; slice that off; repeat until list is empty).
    • Random-shuffle the blocks (Fisher-Yates)
    • Return blocks in the shuffled order

    The answer to any "random, but non-repeating" algorithm is always "shuffle."