
A few days ago I got stuck on a task which required me to make sure the number selection should be random from an array. After lots of searching I decided to take matters into my own hands and started following the algorithm.
The basic Algorithm this post follows is:
This algorithm has been very helpful for my case as this was intended to work for any number of items.
This can be used to draw a winner of any giveaway, to draw a winner for a lottery, this ensures full Randomness of the result as intended.
Let us write a little PHP script following the algorithm. I am using Laravel, PHP for this case, you can use whatever you want..
You need the input array from which you are going to find the random number. The input array can consist of anything whether simple numbers Or JSON objects.
$array = [
{
id:0,
name: name_01
},
{
id:1,
name: name_02
},
{
id:2,
name: name_03
},
{
id:3,
name: name_04
}
];
$array2 = [ 22, 55, 23, 56, 78, 33, 11, 21, 53, 51 ];
Both of these are perfect examples to work in this script.
In PHP you can use the shuffle() function to randomize the array; it will randomly sort the original array and in return will give you a Boolean response.
shuffle($array2); // this will have an output like
//[ 23, 51, 11, 55, 78, 21, 53, 33, 22, 56,]
In PHP you can use the array_slice() function to get only half of the elements of the array. By randomizing the array we have already given equal probability to every number to have a chance of winning. We are just slicing the array into half the size it previously was.
$array2 = array_slice($array2,((sizeof($array2))/2));
// this will have an output like [ 23, 51, 11, 55, 78]
Make a loop run with the condition till the size of the array is one. In the loop’s body randomize the array again and divide it in half.
while( sizeof($array2) > 1 ) // Check if we only have one value left
{
shuffle($array2); //Randomize the array again
$array = array_slice($array,((sizeof($array))/2));
//Divide the array in half
}
You need the input array from which you are going to find the random number. The input array can consist of anything whether simple numbers Or JSON objects.
while( sizeof($array2) > 1 ) // Check if we only have one value left
{
shuffle($array2); //Randomize the array again
$array = array_slice($array,((sizeof($array))/2));
//Divide the array in half
}
After the loop what you will have left with is an array containing only one element which has been randomly chosen from the input array.
$array2 = [ 22, 55, 23, 56, 78, 33, 11, 21, 53, 51 ];
shuffle($array2);
// this will have an output like [ 23, 51, 11, 55, 78, 21, 53, 33, 22, 56,]
$array2 = array_slice($array2,((sizeof($array2))/2));
// this will have an output like [ 23, 51, 11, 55, 78]
while( sizeof($array2) > 1 )
// Check if we only have one value left
{
shuffle($array2); //Randomize the array again
$array = array_slice($array,((sizeof($array))/2));
//Divide the array in half
}
echo($array2);
//this will now only have a single element which is the random Number we wanted to pick
In a lot of businesses, the digital workplace has become an essential part of the work experience. It facilitates a smooth workplace and the free
Point of sale (POS) software is one of the leading segments of software industry. It is implemented and in use of retail industry around the
What is React.js? According to react documentation create React App is an officially supported way to create single-page React applications. Create-React-App is a boilerplate that