2 Years of Service
100%
This is a common question: how to randomize values from a certain set (an array of values) in such a way that none of them are repeated? Today I present the implementation of such an algorithm in PHP and C++. There will also be a bonus in the form of a mini-game written in PHP. Here is the no-repeat randomization algorithm in PHP:
As a result of executing the code, we get an array named $random[] containing 5 unique question numbers (here in indexes from 1 to 5). The key moment of the algorithm is checking each time whether the question number just drawn from the range is not already in the table $drawn[]. If so, the $draw_ok variable is false, which means that the drawn number will not be written to the table a second time. And thanks to the outer do..while loop, there will be another attempt to randomly select a unique number. The same is true in C++. I have prepared two ZIP packages for download, containing in addition to the algorithm a mini demo version (to be able to see the results of the algorithm on the screen).
To see this hidden content, you must like this content.
Last edited by a moderator: