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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:00:19+00:00 2026-06-10T16:00:19+00:00

i’m trying to create a random number generator that will generate number based on

  • 0

i’m trying to create a random number generator that will generate number based on its dynamic probability that keeps changing. Basically the functionality of it is that it will select the most viewed posts from the page to index page and show it. So the page that is most viewed has its counter. On index page i take this counter and want to generate a random number based on the counter that will act as a probability. The higher counter the better chance. My function so far looks like this but i want to know if there is any built in function in PHP or how to make this better and efficient:

private function randomWithProbability($chance, $num, $range = false)
 {
    /* first generate a number from 1 to 100 and see if that number is in the range of chance */
    $rand = mt_rand(1, 100);

    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(0, $range-1);
            while ($rand == $num)
            {
                $rand = mt_rand(0, $range-1);
            }

            return $rand;
        }
    }
 }

$range is just the normal range to generate the number which does not fall in the chance.

Thank you for your help.

  • 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-10T16:00:21+00:00Added an answer on June 10, 2026 at 4:00 pm

    This might be throwing a spanner in the works and completely changing how you intend to implement this. But if i understand correctly what you want to do, could you not use cumulative probability.

    Here’s an example i knocked up in codepad. Obviously my example is simple and could be coded better but it should explain the mechanics of it all. Basically you get the cumulative probability of each “post” based on its view count, then generate a random number. Look at the last array in the output and you can see each post has a chance of being displayed (or whatever you do with it) but the higher viewed ones will have a greater probability.

    http://codepad.org/9a72bezg

    And heres the code just for the sake of it codepad ever goes down.

    <?php
    $posts = array(
     "php article" => 100,
     "c++ article" => 5,
     "monkey article" => 2,
     "another article" => 67,
     "one more" => 87,
     "ok i wanted another" => 34,
     "last one i promise" => 21
    );
    
    // Get total views
    $total_views = array_sum($posts);
    
    echo "total views: ".$total_views."\n\n";
    
    // percentage posts
    $posts_percents = array();
    foreach($posts as $k => $v) {
      $posts_percents[$k] = ($v / $total_views) * 100;
    }
    
    
    //sort um
    asort($posts_percents);
    
    echo "chances of each post being viewed";
    print_r($posts_percents);
    
    //--- work out cumulative percents
    $cumulative = array();
    foreach($posts_percents as $k => $v) {
        $cumulative[$k] = end($cumulative) + $v;
    
    }
    
    echo "the cumulative probability";
    print_r($cumulative);
    
    //--- just a function i will use in the loop below, hopefully being in a function makes it easier to read than if i put it directly in th loop.
    function get_post() {
       global $cumulative;
    
       asort($cumulative);
    
       $lowest = floor(current($cumulative));
    
       $rand = mt_rand($lowest,100);
       foreach($cumulative as $k => $v) {
         if($v > $rand)  {
          // echo $k." (".$rand.")\n";
           return $k;
           }
       }
    
    }
    
    
    $views=array();
    for($i=0;$i<=1000;$i++) {
      $post_viewed = get_post();
    
      if(empty($post_viewed))
        continue;
    
      if(isset($views[$post_viewed]))
         $views[$post_viewed]++;
      else
         $views[$post_viewed]=1;
    
    }
    
    arsort($views);
    
    echo "\n chances of being displayed \n\n";
    print_r($views);
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I need a function that will clean a strings' special characters. I do NOT
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

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.