Jump to content
YOUR-AD-HERE
HOSTING
TOOLS

Locked DRAWING WITHOUT REPEATS IN PHP AND C++. THE MILLIONAIRES GAME


avolfer

Recommended Posts

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:

This is the hidden content, please

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).

Edited by itsMe
use HIDE TAGS
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.