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

  • SEARCH
  • Home
  • 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 7673447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:30:49+00:00 2026-05-31T16:30:49+00:00

I need a function that randomizes an array similar to what shuffle does, with

  • 0

I need a function that randomizes an array similar to what shuffle does, with the difference that each element has different chances.

For example, consider the following array:

$animals = array('elephant', 'dog', 'cat', 'mouse');

elephant has an higher chance of getting on the first index than dog. Dog has an higher chance than cat and so on. For example, in this particular example elephant could have a chance of 40% in getting in the 1st position, 30% of getting on the 2nd position, 20% on getting on 3rd and 10% getting on last.

So, after the shuffling, the first elements in the original array will be more likely (but not for sure) to be in the first positions and the last ones in the last positions.

  • 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-31T16:30:51+00:00Added an answer on May 31, 2026 at 4:30 pm

    Normal shuffle may be implemented just as

    • dropping items randomly at some range
    • picking them up from left to right

    We can adjust dropping step, drop every element not into whole range, but at some sliding window. Let N would be amount of elements in array, window width would be w and we’ll move it at each step by off. Then off*(N-1) + w would be total width of the range.

    Here’s a function, which distorts elements’ positions, but not completely at random.

    function weak_shuffle($a, $strength) {
        $len = count($a);
        if ($len <= 1) return $a;
        $out = array();
        $M = mt_getrandmax();
        $w = round($M / ($strength + 1)); // width of the sliding window
        $off = ($M - $w) / ($len - 1); // offset of that window for each step.
        for ($i = 0; $i < $len; $i++) {
            do {
                $idx = intval($off * $i + mt_rand(0, $w));
            } while(array_key_exists($idx, $out));
            $out[$idx] = $a[$i];
        }
        ksort($out);
        return array_values($out);
    }
    
    • $strength = 0 ~normal shuffle.
    • $strength = 0.25 ~your desired result (40.5%, 25.5%, 22%, 12% for elephant)
    • $strength = 1 first item will never be after last one.
    • $strength >= 3 array is actually never shuffled

    Playground for testing:

    $animals = array( 'elephant', 'dog', 'cat', 'mouse' );
    $pos = array(0,0,0,0);
    for ($iter = 0; $iter < 100000; $iter++) {
        $shuffled = weak_shuffle($animals, 0.25);
        $idx = array_search('elephant', $shuffled);
        $pos[$idx]++;
    }
    print_r($pos);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a function that I can call when somthing is done, for example
I need a function that can return the difference between the below two dates
I need a function that will convert a type to a string, for example:
I need a function that returns non-NaN values from an array. Currently I am
I need a function that should give me a 10th or 100th array, for
I need a function that will do something like this: $arr = array(); //
somethings somethings somethings somethings i need a function that check the next element in
I'm learning R and I'm curious... I need a function that does this: >
$arr1 = array('potato'=>1,'tomato'=>2,'apple'=>5,'banana'=>10); $arr2 = array('orange'=>20,'tomato'=>3,'apple'=>5,'banana'=>20); I need function that would return array('tomato','banana'), consider
I need a function that gets two array parameters of the same length: property-names

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.