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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:03:34+00:00 2026-06-05T06:03:34+00:00

In a browser game we have items that occur based on their probabilities. P(i1)

  • 0

In a browser game we have items that occur based on their probabilities.

P(i1) = 0.8

P(i2) = 0.45

P(i3) = 0.33

P(i4) = 0.01

How do we implement a function in php that returns a random item based on its probability chance?

edit

The items have a property called rarity which varies from 1 to 100 and represents the probability to occcur. The item that occurs is chosen from a set of all items of a certain type. (e.x the given example above represents all artifacts tier 1)

  • 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-05T06:03:35+00:00Added an answer on June 5, 2026 at 6:03 am

    I don’t know if its the best solution but when I had to solve this a while back this is what I found:

    Function taken from this blog post:

    // Given an array of values, and weights for those values (any positive int) 
    // it will select a value randomly as often as the given weight allows. 
    // for example:
    // values(A, B, C, D)
    // weights(30, 50, 100, 25)
    // Given these values C should come out twice as often as B, and 4 times as often as D. 
    function weighted_random($values, $weights){ 
        $count = count($values); 
        $i = 0; 
        $n = 0; 
        $num = mt_rand(0, array_sum($weights)); 
        while($i < $count){
            $n += $weights[$i]; 
            if($n >= $num){
                break; 
            }
            $i++; 
        } 
        return $values[$i]; 
    }
    

    Example call:

    $values = array('A','B','C');
    $weights = array(1,50,100);
    
    $weighted_value = weighted_random($values, $weights);
    

    It’s somewhat unwieldy as obviously the values and weights need to be supplied separately but this could probably be refactored to suit your needs.

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

Sidebar

Related Questions

Background I have an existing extension designed to accompany a browser-based game (The extension
For my browser based Flex game (for the Facebook platform), I am using PHP
I'm trying to create a PHP based game where you have to do all
I have very recently started development on a multiplayer browser game that will use
Lets say that in a browser based game, completing some action (for simplicity lets
I am developing a browser based game, and I have a big map there.
I work on a hexagon map based browser game. I have this: http://www.dark-project.cz/wesnoth/map-view/1 and
I have a browser game I made that runs in MVC3 .Net. Because everyone
I have a Flash based game for the browser which sends users' scores to
I would like to build web browser game using php. I need graphics so

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.