Generate random numbers without repeating a value

From Rosetta Code
Revision as of 16:55, 24 August 2021 by Puppydrum64 (talk | contribs) (Created page with "{{draft task}} Many puzzle games such as the 15 puzzle game need a way to randomize the order of the pieces. One way to do this is to create an array and fill it with ran...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Generate random numbers without repeating a value is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Many puzzle games such as the 15 puzzle game need a way to randomize the order of the pieces. One way to do this is to create an array and fill it with random values, with each element's index in that array being its position. Unfortunately, most random number generators can produce the same value more than once, which in this case isn't what we want.


Task

Create a random number generator and have it produce 20 values. It cannot produce the same value more than once. Or:

Given the output of an existing random number generator that does produce repeated output, create a function that alters the output in such a way that no value is repeated. (Technically it stops being "random" at that point, but that's beyond the scope of this task.) Try your best not to make the process take too long at runtime.

For the second version of the task, the random number generator itself need not be implemented; however you must specify its possible range of values. (e.g "Assume the random number generator creates a value from 0 to 255")

Related Tasks