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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:09:20+00:00 2026-06-13T15:09:20+00:00

I have a list of numbers like $list = array(1,5,19,23,59,51,24) in actual code this

  • 0

I have a list of numbers like

 $list = array(1,5,19,23,59,51,24) 

in actual code this is generated from database, so this array will hold up to 500 numbers that are different from each other.

each of these numbers in the database has a probability of occurring recorded. So i have a data from previous executions to generate random numbers from 1 to 500 and recorded the probabilities of each number generated for like 1000 times.

Now having list of numbers and probabilities for each number i want to write a function that will generate a random number from these 500 numbers based on their probabilities.

For example:

    number 1 has a chance of: 0.00123 //0.123%
    number 6 has a chance of: 0.0421 //4.21%
    number 11 has a chance of: 0.0133 //1.33%

so variable $finallist will look something like this:

   $finallist[1] = 0.00123;
   $finallist[6] = 0.0421;
   $finallist[11] = 0.0133;

Now if i run my function and pass in $finallist as a parameter i want to retrieve a random number between 1 and 6 but number 6 will have higher possibility of coming out than 1 and 11 will have higher possibility to come out than 1.

I have some functions written that deal with returning the random number based on its chance but it only takes 1 value as a parameter.

private function randomWithProbability($chance, $num, $range = false)
{
    /* first generate a number 0 and 1 and see if that number is in the range of chance */
    $rand = $this->getRandomFloatValue(0, 1);

    if ($rand <= $chance) 
    {
        /* the number should be returned */
        return $num;
    }
    else 
    {
        /* otherwise return a random number */
        if ($range !== false)
        {
            /* make sure that this number is not same as the number for which we specified the chance */
            $rand = mt_rand(1, $range);
            while ($rand == $num)
            {
                $rand = mt_rand(1, $range);
            }

            return $rand;
        }
    }
}

if anyone knows a solution/algorithm to do this or if there is anything built in to PHP would be a big help. Thank you so much.

  • 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-13T15:09:22+00:00Added an answer on June 13, 2026 at 3:09 pm

    The basic algorithm you’re looking for:

    • add all the probabilities together and determine the maximum
    • pick a random number between 0 and 1 and multiply it by the max
    • find the entry that corresponds with that value

    Example code:

    <?php
    
    // create some weighted sample data (id => weight)
    $samples = array(
      'a' => 0.001,
      'b' => 0.004,
      'c' => 0.006,
      'd' => 0.05,
      'e' => 0.01,
      'f' => 0.015,
      'g' => 0.1
    );
    
    class Accumulator {
       function __construct($samples) {
          // accumulate all samples into a cumulative amount (a running total)
          $this->acc = array();
          $this->ids = array();
          $this->max = 0;
          foreach($samples as $k=>$v) {
             $this->max += $v;
             array_push($this->acc, $this->max);
             array_push($this->ids, $k);
          }
       }
    
       function pick() {
          // selects a random number between 0 and 1, increasing the multiple here increases the granularity
          // and randomness; it should probably at least match the precision of the sample data (in this case 3 decimal digits)
          $random = mt_rand(0,1000)/1000 * $this->max;
          for($i=0; $i < count($this->acc); $i++) {
             // looks through the values until we find our random number, this is our seletion
             if( $this->acc[$i] >= $random ) {
                return $this->ids[$i];
             }
          }
          throw new Exception('this is mathematically impossible?');
       }
    
       private $max; // the highest accumulated number
       private $acc; // the accumulated totals for random selection
       private $ids; // a list of the associated ids
    }
    
    $acc = new Accumulator($samples);
    
    // create a results object to test our random generator
    $results = array_fill_keys(array_keys($samples), 0);
    
    // now select some data and test the results
    print "picking 10000 random numbers...\n";
    for($i=0; $i < 10000; $i++) {
       $results[ $acc->pick() ]++;
    }
    
    // now show what we found out
    foreach($results as $k=>$v) {
       print "$k picked $v times\n";
    }
    

    The results:

    > php.exe rand.php
    picking 10000 random numbers...
    a picked 52 times
    b picked 198 times
    c picked 378 times
    d picked 2655 times
    e picked 543 times
    f picked 761 times
    g picked 5413 times
    

    Running the same code with this sample:

    // samples with even weight
    $samples = array(
       'a' => 0.1,
       'b' => 0.1,
       'c' => 0.1,
       'd' => 0.1
    );
    

    Produces these results:

    > php.exe rand.php
    picking 10000 random numbers...
    a picked 2520 times
    b picked 2585 times
    c picked 2511 times
    d picked 2384 times
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file that contain list of numbers that looks like this: 10^-92
I have a list of numbers like this (array) 1 2 3 4 So
If I have a list of numbers like this one: 10,9,8,8,9,7,6,5,4,6,7,8,11,10,12,14,16,20,30,29,28,29,27,25,20,18,15,10,8,5,4,1 I want to
I have a list of numbers which looks like this: 1.234D+1 or 1.234D-02 .
I have a list of ID numbers that I pull from a session variable
Hi I have an array list which has some numbers in it like {23,16,45,26,2,5,9}
I have a list of numbers on a table like this: n | Title---
I have a List with numbers, and I'd like to find the position of
I have a list of numbers ( integers ) (say, from 1 to 10).
I have a list of phone numbers that have been dialed (nums_dialed). I also

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.