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 7726101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:07:47+00:00 2026-06-01T05:07:47+00:00

I have the following array $arr = array( person1 => 10, person2 => 10,

  • 0

I have the following array

$arr = array(
    "person1" => 10,
    "person2" => 10,
    "person3" => 20,
    "person4" => 25,
    "person5" => 35,                    
);

I would like to write a function that takes $arr as an argument and returns 3 elements of the array based on the values stored in each element.

For example if the returned subset yields

$newArr = array(
    "person5" => 35,  
    "person1" => 10,
    "person4" => 25,                  
);

There was a 35% chance that person5 would be the first element stored in $newArr based on the value stored in $arr['person5'] divided by the sum of values stored in the remaining elements.
$arr['person5']/($arr['person5'] + $arr['person4'] + $arr['person3'] + $arr['person2'] + $arr['person1'])

There was a ~15% chance that person1 would be the second element stored in $newArr based on the value stored in $arr['person1'] divided by the sum of values stored in the remaining elements.
$arr['person1']/($arr['person4'] + $arr['person3'] + $arr['person2'] + $arr['person1'])

There was a ~45% chance that person4 would be the second element stored in $newArr based on the value stored in $arr['person4'] divided by the sum of values stored in the remaining elements.
$arr['person4']/($arr['person4'] + $arr['person3'] + $arr['person2'])

How could I write a function that does this?

  • 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-06-01T05:07:49+00:00Added an answer on June 1, 2026 at 5:07 am

    You’re looking for a roulette wheel selection algorithm, see: http://en.wikipedia.org/wiki/Fitness_proportionate_selection

    $count = 3;
    $arr = array(
        "person1" => 10,
        "person2" => 10,
        "person3" => 20,
        "person4" => 25,
        "person5" => 35,                    
    );
    
    $result = array();
    
    // sort from low to high
    asort($arr);
    
    // loop 3 times (based on count)
    while ($count > 0){
    
        // get the sum of all persons
        $sum = 0;
        foreach ($arr as $rank){
            $sum += $rank;
        }
    
        // get a random value between 0 and sum
        $delta = rand(0, $sum);
        $current = 0;
    
        // keep looping over each item, increasing rank untill $sum has surpassed delta
        // see each item as as person containing a portion of the slice. The bigger the value, the greater the change of being selected
        $current = 0;
        foreach ($arr as $name => $rank){
            $current += $rank;
    
            if ($delta <= $current){
                $result[$name] = $rank;
                break;
            }
        }
    
        unset($arr[$name]);
    
        $count--;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have an array arr . When would the following not give
Suppose that I have the following c function: int MyFunction( const float arr[] )
I have a string indexed array that I would like to remove an item
I have following array, that I need to operate by hand on bitmaps. const
I have the following two dimensional array: static int[,] arr = new int[5, 5]
I have the following code: Function TruncateString(str, n) ' Returns an array with strings
I have the following : <script> $().ready(function() { $(#myVal#).blur(function() { var arr = jQuery.makeArray(
Let's say I have the following array: arr = [, 2121, 8, myString] I
I have the following code: function lower_than_10($i) { return ($i < 10); } that
I have an array with arrays of two elements. Now I would like to

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.