Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6677657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:08:55+00:00 2026-05-26T04:08:55+00:00

I need to shuffle an array based on a seed number so I can

  • 0

I need to shuffle an array based on a seed number so I can get the same shuffle if I need it.

For example:

1. print_r( shuffleIt( $array, 2 ) );
2. print_r( shuffleIt( $array, 6 ) );
3. print_r( shuffleIt( $array, 2 ) );
  1. and 3. would show the same shuffled array but different than 2.

I found this function googling:

function entropy( $array, $sort_seed ) {
    mt_srand( $sort_seed );
    $order = array_map( create_function( '$val', 'return mt_rand( );' ), range( 1, count( $array ) ) );
    array_multisort( $order, $array );
    return $array;
}

It works fine on my pc with php-cli, I always get the same array for each different sort_seed I use, but when I uploaded it to a server, I get different arrays every time even when I am using the same sort_seed.

How could I get always the same shuffled array when using the same sort_seed?

btw. I need to preserve keys or sort a multidimensional array so I can store the key there.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:08:56+00:00Added an answer on May 26, 2026 at 4:08 am

    If you need it on any pc, you need a random number generator that works the same across computers. I looked up one pseudo-random-number generator that might be suitable for you from the Random Number Generation Wikipedia page and put it into a class so you can seed it.

    I don’t know for what you need it, but this just might suit your needs. It’s independent to system configuration:

    function shuffleIt($array, $seed)
    {
        $mwc = new mwc($seed);
        $order = array();
        $count = count($array);
        while($count--)
            $order[] = $mwc->random()
        ;
    
        array_multisort($order, $array);
        return $array;
    }
    
    /**
     * Multiply-with-carry RNG
     * 
     * method invented by George Marsaglia
     */
    class mwc
    {
        private static $def_m_w = 1712; /* must not be zero */
        private static $def_m_z = 23;   /* must not be zero */
        private $m_w, $m_z;
        public function __construct($seed = NULL)
        {
            $this->m_w = self::$def_m_w;
            $this->m_z = self::$def_m_z;
            if (NULL !== $seed)
                $this->seed($seed);
        }
        public function seed($seed)
        {
            $seed = (int) $seed;
            if (!$seed) throw new InvalidArgumentException('Must not be zero.');
            $this->m_z = $seed;
            $this->random();
        }
        public function random()
        {
            $this->m_z = 36969 * ($this->m_z & 65535) + ($this->m_z >> 16);
            $this->m_w = 18000 * ($this->m_w & 65535) + ($this->m_w >> 16);
            return ($this->m_z << 16) + $this->m_w;  /* 32-bit result */
        }
    }
    

    Note: This might behave differently between 32/64 bit systems, especially as PHP differs here for integers and overflows between windows and unix. You might want offset at the signed minimum for 32 bit integers in PHP instead of 0 as it is now, to switch the implementation to gmp or just reduce the size by one bit.


    Usage Example 32 bit reported to work by ekke from netherlands

    $shuffle = new GeorgeShuffle();
    $seed    = $shuffle->seed();
    $a       = array('A', 'B', 'C', 'D', 'E', 'F', 'G');
    $shuffle->reOrder($a);
    var_dump($a);
    $shuffle->seed($seed);
    $shuffle->reOrder($a);
    var_dump($a);
    
    /**
     * Array shuffle class using
     * the multiply-with-carry method
     * invented by George Marsaglia
     */
    class GeorgeShuffle
    {
    
        private static $def_m_w = 1959; /* must not be zero */
        private static $def_m_z = 2006; /* must not be zero */
        private $m_w, $m_z;
        const maxint = 2147483647;
    
        public function __construct($seed = null)
        {
            $this->m_w = self::$def_m_w;
            $this->m_z = self::$def_m_z;
            if ($seed) $this->seed($seed);
        }
    
        public function reOrder(&$array, $seed = null)
        {
            if (!empty($seed)) $this->seed($seed);
            $a = array();
            for ($i = 0, $j = count($array); $i < $j; $i++) {
                $a[$i] = $this->random();
            }
            array_multisort($a, $array);
            //- to return a copy, remove the &
            return $array;
        }
    
        public function seed($seed = false)
        {
            if (is_string($seed)) $seed = hexdec($seed);
            if (empty($seed)) $seed = round(mt_rand(1, self::maxint));
            $this->m_z = $seed;
            $this->random();
            //- return the seed used in hex (8 chars) for reproducing
            return str_pad(dechex($seed), 8, '0', STR_PAD_LEFT);
        }
    
        public function random()
        {
            $this->m_z = 36969 * (($this->m_z And 65535) + ($this->m_z >> 16));
            $this->m_w = 18000 * (($this->m_w And 65535) + ($this->m_w >> 16));
            return ($this->m_z << 16) + $this->m_w; /* 32-bit signed result */
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to randomly shuffle the following Array: int[] solutionArray = {1, 2, 3,
Need to call a filter function on some options based on a radio selected
Need to convert a string to normal datetime format so it can be correctly
I'm building a socket application that need to shuffle a lot of small/medium sized
I have an unordered list. When i click a shuffle button, i need: the
I have an array in my PHP application, and I need to develop a
I want to show a random record from the database. I would like to
I have a SQL database that contains a field. I can get all of
I am wondering if the shuffle() array function is the correct way to randomize
Possible Duplicate: How exactly should I implement a shuffle or random-number algorithm for an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.