Pick a Random Number from any Sequence

A simple Algorithm

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:

  • Get the input array
  • Randomize the array.
  • Divide it into 2 parts and chose one
  • Follow the given steps till you only have one item lift.

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

Step 1: Input Array

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.

Step 2: Randomizing the Array

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,]

Step 3: Get only half of the array

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]

Step 4: Repeat the steps till you are left with only ONE value

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.

Final Look at the script

$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

5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Let’s Socialize

Popular Post

Share

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on tumblr
Tumblr
Share on skype
Skype
Share on whatsapp
WhatsApp

Difference between React.js & Next.js

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

Read More »
0
Would love your thoughts, please comment.x
()
x