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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:01:10+00:00 2026-06-06T18:01:10+00:00

Why is variance big enough of random by probability result? Test code: function probability($chances)

  • 0

Why is variance big enough of random by probability result?

Test code:

function probability($chances) {

    asort($chances);
    $sum    = array_sum($chances);
    $random = mt_rand(1, $sum);

    foreach($chances as $key => $chance) {
        if($random < $chance)
            return $key;
    }

    return $key;

}

$chances['case1'] = 10;
$chances['case2'] = 30;
$chances['case3'] = 60;

$result = array();

for($i = 0; $i < 100000; $i++)
    @$result[probability($chances)]++;

asort($result);
$sum = array_sum($result);

echo "Case\tCount\tOrig\tResult\n";

foreach($result as $key => $value)
    echo "$key\t$value\t".$chances[$key]."%\t".round($value / $sum * 100)."%\n";

Result:

Case    Count   Orig    Result
case1   14913   10%     15%
case2   33099   30%     33%
Case3   51988   60%     52%

Is it possible to adjust it somehow? I was trying to use mt_srand(), but it doesn’t help.

Info:

$ php -v
PHP 5.3.10-1ubuntu3.2 with Suhosin-Patch (cli) (built: Jun 13 2012 17:20:55) 
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies
    with Xdebug v2.1.0, Copyright (c) 2002-2010, by Derick Rethans
    with Suhosin v0.9.33, Copyright (c) 2007-2012, by SektionEins GmbH

$ uname -a
Linux desktop 3.2.0-26-generic-pae #41-Ubuntu SMP Thu Jun 14 16:45:14 UTC 2012 i686 i686 i386 GNU/Linux
  • 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-06T18:01:12+00:00Added an answer on June 6, 2026 at 6:01 pm
    $sum    = max($chances);
    

    max() dosn’t sum, use array_sum() insted

    I got this result:

    Case    Count   Orig    Result
    case1   11068   10%     11%
    case2   29672   30%     30%
    case3   59260   60%     59%
    

    From running this version of your code:

    <?php
    
    function probability($chances)
    {
        asort($chances);
        $sum    = array_sum($chances);
        $random = mt_rand(1, $sum);
    
        foreach($chances as $key => $chance)
        {
            $random -= $chance;
            if($random <= 0)
            {
                return $key;
            }
        }
    
        return $key;
    }
    
    $chances['case1'] = 10;
    $chances['case2'] = 30;
    $chances['case3'] = 60;
    
    $result = array();
    
    for($i = 0; $i < 100000; $i++)
    {
        @$result[probability($chances)]++;
    }
    
    asort($result);
    $sum = array_sum($result);
    
    echo "Case\tCount\tOrig\tResult\n";
    
    foreach($result as $key => $value)
    {
        echo "$key\t$value\t".$chances[$key]."%\t".round($value / $sum * 100)."%\n";
    }
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my Code: var dsdata = ibll.showPartnumberandVariance(ient); data result: PartNumber Variance 001 1
It seems like the na.rm=T within the apply function in my variance calculation: poolvarcheck=((7037*(apply(yes,
I am trying to convert the following code for the variance calculation public static
I need to test if one variance matrix is diagonal. If not, I'll do
I need to code a Maximum Likelihood Estimator to estimate the mean and variance
Given a mean and a variance is there a simple function call which will
Is there any method in java that is equivalent to numpy.random.normal(mean,variance) .
am trying to calculate mean and variance using 3X3 window over image(hXw) in opencv...here
@uncheckedVariance can be used to bridge the gap between Scala's declaration site variance annotations
I want to do a Mean-Variance-Optimization (Markowitz) but i never found anything written in

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.