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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:17:55+00:00 2026-05-28T03:17:55+00:00

This is really more a question of approach, but I’m presenting it in php.

  • 0

This is really more a question of approach, but I’m presenting it in php.

Suppose we had a list of four percentages that a give event will occur on iteration.

array=('walk the dog'=>.25,'read the paper'=>.25,'drink coffee'=>.0,'listen to music'=>.50)

(Keys are just tests – in practice this is going to be used in a strategy pattern that applies different methods to an object off chance using call_user_func())

In a loop, what would be the best way to choose one of these events by random-adjusted selection taking their respective weights into account (IE, for this particular configuration, there will be a lot of music listened to and not a lot of coffee drank)

Currently I have something like:

for($i=1;$i<=3;$i++){
  $rand = 1/rand(1,10);
  //choose from the list
}

But this would require mapping the percentages to value ranges (50-100% become listening to music, 0-25% walking the dog and 25-50 reading the paper) because the ‘value’ for that iteration is calculated as a floating point number between 0 and 1. This kind of changes the way the percentages need to be recorded.

Is there any kind of approach that would preserve the identity of the values as the real probability they should be chosen rather than ranges? It would definitely make the code more configurable since I have to make these values adjustable. Please let me know if I’ve been too vague.

  • 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-28T03:17:56+00:00Added an answer on May 28, 2026 at 3:17 am

    I’d do something like:

    $arr = array (
        'walk the dog'    =>  array('min' =>  0, 'max' =>  25),
        'read the paper'  =>  array('min' => 25, 'max' =>  50),
        'drink coffee'    =>  array('min' =>  0, 'max' =>   0),
        'listen to music' =>  array('min' => 50, 'max' => 100),
    );
    
    $rnd = rand(1,100);
    foreach($arr as $k =>$v) {
        if ($rnd > $v['min'] && $rnd <= $v['max']) {
            echo $k,"\n";
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is really more of a serverfault/IT question, but I am not part of
This isn't really a programming question but more about programming and testing tools. Is
This is more a style question. For CPU bound processes that really benefit for
There are a few questions that approach an answer to this question, but none
This is not really a programming question, more of an algorithmic one. The problem:
This is more of a licencing issue than a code question. I really like
This is a pretty simple question really, but let's say I'm creating a model
I think this is more like Math question rather than programming but since I'm
This is a moot question as I'm not on this project any more, but
This has been asked before (question no. 308581) , but that particular question and

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.