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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T18:45:55+00:00 2026-05-21T18:45:55+00:00

An array shall be sorted high to low by its values. <?php $items =

  • 0

An array shall be sorted high to low by its values.

<?php
$items = array(
  1 => f(1),
  2 => f(2),
  3 => f(3),
  4 => f(4),
  5 => f(5),
);
?>

After sorting I look which item 1, 2, 3, 4, 5 is the first one. I try that again and again and again.
Afterwards

  • 5 should be the first item five times more than 1
  • 4 should be the first item four times more than 1
  • 3 should be the first item three times more than 1
  • 4 should be the first item two times more than 2
  • …

One idea is

<?php
function f(key) {
  return key / random();
}
?>

which, for 1’000’000 tries resulted in

key | times on top | ratio with key one | expected ratio
----+--------------+--------------------+---------------
 5  |      374'365 | 6.75               | 5
 4  |      267'863 | 4.83               | 4
 3  |      185'707 | i am so lazy ...   | 3
 2  |      116'618 |                    | 2
 1  |       55'447 | 1                  | 1

Looks wierd to me, but maybe

  • there is a simple problem with f?
  • there is a better f?


My implementation:

<?php

abstract class Test {

  private $result;

  protected abstract function f($x);

  protected function iteration() {
    $values = array(
      1 => $this->f(1),
      2 => $this->f(2),
      3 => $this->f(3),
      4 => $this->f(4),
      5 => $this->f(5),
    );

    arsort($values);

    $top = key($values);

    if (!isset($this->result[$top])) {
      $this->result[$top] = 1;
    } else {
      $this->result[$top]++;
    }
  }

  public function run($iterations) {
    $this->result = array();
    for($i = 0; $i < $iterations; $i++) {
      $this->iteration();
    }
    arsort($this->result);
    return $this->result;
  }
}

class MyTest extends Test {
  protected function f($x) {
    return $x / rand();
  }
}

$test = new MyTest();
$result = $test->run(1000 * 1000);
print_r($result);
printf("Ratio of key 5 to 1, which should be 5: %f\n", $result[5] / $result[1]);

?>

I have tried a billion rounds. But again the ratio is 6.75 – the whole point is: why isn’t it five?


The results for

<?php
class BetterRandomGeneratorTest extends Test {
  protected function f($x) {
    return $x / mt_rand();
  }
}
?>

are

Array
(
  [5] => 3742816
  [4] => 2674352
  [3] => 1861444
  [2] => 1168333
  [1] => 553055
)
Ratio of key 5 to 1: 6.767529
  • 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-21T18:45:55+00:00Added an answer on May 21, 2026 at 6:45 pm

    Here is a simple f which will do it.

    function f(key) {
      $x = 0;
      for($i = 0; $i < $key; $i++) {
        $y = random();
        if ($x < $y) {
          $x = $y;
        }
      }
      return $x;
    }
    

    This is guaranteed to work because the max is equally likely to be any of the 15 random numbers chosen, and 1/3 of the time that number will be in f(5), versus 1/15 for f(1).

    As for what was wrong with your f, it is quite simple. Your solution has the nice symmetry that exactly 80% of the time, f(1) < f(5). However f(1) tends to be bigger than f(5) when f(1) is larger than average and f(5) is smaller than average. Ditto for f(2), f(3) and f(4). However it is unusual for all of f(2), ... f(5) to be small at once. This causes correlations that cause f(1) to be the largest less often than you would naively think. Vice versa correlations tend to come out in favor of f(5) more often than you would naively think.

    If you want to compute the exact probabilities of each number coming out on top, it shouldn’t be too hard to compute exact answers with integration. The idea is that you integrate from 0 to 1 the probability that, if that was the value of random() for f(i) that f(i) is the maximum. (So, for instance, for 5 you would integrate (1-x/5)(1-x/4)(1-x/3)(1-x/2) while for 1 you would integrate a function that is 0 if random() is bigger than 0.2, and otherwise is (1-2x)(1-3x)(1-4x)(1-5x).) The expressions will be complicated, and the ratios won’t come out to nice answers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to sort values of an array in alphabetical order in PHP. If
I have an array of buttons which shall all call the same method but
I have an array that after being sorted appears like this: var arr =
When shall i use malloc instead of normal array definition in C? I can't
I want to create an array which can be extended at any time by
I just wrote a delegate method, that shall return an array, I've created inside
I have a function that expects an array. This array shall be printed out
I have a static class 'Defaults' which shall hold default matrices that are forwarded
I got a cell in my table called platform_rating which shall be the percentual
What is the most efficient algorithm for grouping identical items together in an array,

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.