Current location: Hot Scripts Forums » Programming Languages » PHP » Weighted random function


Weighted random function

Reply
  #1 (permalink)  
Old 05-07-09, 01:41 AM
Nikas Nikas is offline
Coding Addict
 
Join Date: Jun 2005
Location: Singapore
Posts: 377
Thanks: 0
Thanked 1 Time in 1 Post
Weighted random function

Hi all,

I'm in need of this particular function for my program. Weighted random function which basically pick out a random element inside a array base on the weight of the element.

PHP Code:

// Example

$element = array('one''two''three');
$weight = array('0.5''0.65''0.8');

// Or it could be in another way.
$element_weight = array('one' => '0.5''two' => '0.65''three' => '0.8'); 
And base on the function, randomly select one element out from the array base on the probability that it might be chosen. I've did some research through the internet and found two that would possible be what I need. You could refer to Random-Weighted-Element-In-PHP and The Forge of the best Weighted Random function.

I thought that either one would matches what I need. And I'm not sure which one would better suit mine. And also, I have to tweak a little to suit my program. Anyone could give me advise on this or maybe suggest another function that maybe is even better than the above two.

Thank for any input.
Reply With Quote
  #2 (permalink)  
Old 05-07-09, 04:38 AM
dgreenhouse's Avatar
dgreenhouse dgreenhouse is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: San Francisco
Posts: 457
Thanks: 0
Thanked 3 Times in 3 Posts
I'd probably pick Jesse Farmer's algorithm as it's the simplest to understand. Plus, it works.

I guess the only other question would be: Is there some inherent weighting associated with the values that you'll be selecting?

That is, for each value in the 'selected values' array, is there some natural weighting -
like frequency as in words in the language, letters in text, numeric patterns in a series, etc.?

And if so, can this natural weighting be easily delineated and segmented in a 'weightings' array?

If the answer is in the affirmative, then I'd consider ordering the weightings array
based upon the discovered weightings and still use Farmer's algorithm -
with the discovered weightings as array element values.

Last edited by dgreenhouse; 05-07-09 at 04:48 AM.
Reply With Quote
  #3 (permalink)  
Old 05-07-09, 07:04 AM
Nikas Nikas is offline
Coding Addict
 
Join Date: Jun 2005
Location: Singapore
Posts: 377
Thanks: 0
Thanked 1 Time in 1 Post
I'm confused of what you have said.

There will be a list of names from my array, and each of the names inside the array would have different weight given to each name. However, some names may have the same weight. It is base on some result I have set.

For example, I have 5 name here in a array.
PHP Code:

$names = array('Tom''Jerry''Joe''Nick''Paul'); 

Before assigning a weight to each of the name
I have to check something before assigning the weight to them. I will start off with a standard weight for each person say 0.8 for all people.
Next I will check whether they have perform a duty in the previous month. If so, I will -0.1 from people whom have perform the duty in the previous month. Next, I have to check whether is it a weekend. If so, I will -0.4 (excluding -0.1 previously). Thus, the chances to select this person for current month duty would be 0.3. Compare to a person whom had perform a duty the previous month and it is on a weekday, the chance would be like 0.55.

And thus, I would like to select the name for the next month duty based on the weight of each. Thus, after calculating for everyone.

PHP Code:

$names = array('Tom''Jerry''Joe''Nick''Paul');
// Eg of the weight for each of them.
$weight = array('0.8''0.55''0.3''0.55''0.8'); 
And to say. Tom and Paul would certainly have a higher chance to being selected rather than Joe. Am I clear about this, or making you more confused of what I want.
Reply With Quote
  #4 (permalink)  
Old 05-07-09, 10:53 PM
dgreenhouse's Avatar
dgreenhouse dgreenhouse is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: San Francisco
Posts: 457
Thanks: 0
Thanked 3 Times in 3 Posts
I'm assuming that this is part of the same app we discussed a few weeks back.

I'd say just create some test data and try out your formulating theories.

Or to paraphrase Nike: "Just Say Go!" Vroooomm...
Reply With Quote
  #5 (permalink)  
Old 05-08-09, 10:00 PM
infinitylimit's Avatar
infinitylimit infinitylimit is offline
Code Guru
 
Join Date: Jun 2004
Location: Oregon
Posts: 758
Thanks: 0
Thanked 0 Times in 0 Posts
Here is what I would do as a hack without testing it
PHP Code:

$names = array('tom','dick','joe','jane','mary');
$weighted_array = array();
$weight = array(80,55,30,55,80);
for(
$i=0;$i<count($weight);$i++){
       
$some_array array_fill(0,$weight[$i],$names[$i]);
       
array_merge($weighted_array,$some_array);
}
array_rand($weighted_array);
array_pop($weighted_array): 
Basically just build an array of 80 toms, 55 dicks etc... then randomize the array and pop your element. A hack but will work.
__________________
Hawk Enterprises -- Home to PHP games, open-source code, tutorials and free downloads

Last edited by infinitylimit; 05-08-09 at 10:11 PM.
Reply With Quote
  #6 (permalink)  
Old 05-09-09, 12:24 PM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
@Infinitylimit: but that's very inefficient. I didn't have a solution but this had me thinking. I had an idea although it might not be truly random but you can generate random numbers between 0 and 1 and then add each one of those numbers to each weight respectively. Then simply take the element with the maximum weight. Now, logically, the one with the "heaviest" weight has higher chances of being the maximum.
PHP Code:

<?php
$arr_val 
= array('Tom''Mike''Hades');
$arr_weight = array(0.350.650.8);

for (
$i 0$i count($arr_weight); $i++)
{
    
$arr_weight_2[$i] = $arr_weight[$i] + (mt_rand(0,2e9)/2e9);
}

// find the key to the maximum weight value after adding the random number
$max array_search(max($arr_weight_2), $arr_weight_2);
echo 
$arr_val[$max];
?>
HTH

Edit: After actually reading your post I think I might have jumped the shark. :\
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]

Last edited by NeverMind; 05-09-09 at 12:33 PM.
Reply With Quote
  #7 (permalink)  
Old 05-09-09, 02:23 PM
infinitylimit's Avatar
infinitylimit infinitylimit is offline
Code Guru
 
Join Date: Jun 2004
Location: Oregon
Posts: 758
Thanks: 0
Thanked 0 Times in 0 Posts
@NVM
<fonz>Ahyee!</fonz>
Yeah I was trying to say by hack I meant it isn't the most effiecent but I knew it would work as intended.
However, so like how fast is array_search on large sets?

Last night I was thinking about this in a general form and I kept coming back to a matrix or result set for a weighted algorithm. By dropping 1 precision to 1-10 I think that would create a working constraint.
__________________
Hawk Enterprises -- Home to PHP games, open-source code, tutorials and free downloads
Reply With Quote
  #8 (permalink)  
Old 05-10-09, 03:06 AM
Nikas Nikas is offline
Coding Addict
 
Join Date: Jun 2005
Location: Singapore
Posts: 377
Thanks: 0
Thanked 1 Time in 1 Post
@dgreenhouse: Alright. I guess I will try out his method first and test out to see if it works for me.

@Infinitylimit: Thanks for coding an example for me, but how is it different from Jesse Farmer one?
Reply With Quote
  #9 (permalink)  
Old 05-10-09, 07:26 AM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
Quote:
Originally Posted by Nikas View Post
I'm in need of this particular function for my program. Weighted random function which basically pick out a random element inside a array base on the weight of the element.
This may or may not be what you're looking for. The code will probably need to be modified for your application, but this could give you a starting point:

PHP Code: Weighted Random Choice - Random selection with a weighted bias.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Reply With Quote
  #10 (permalink)  
Old 05-10-09, 08:34 AM
Nikas Nikas is offline
Coding Addict
 
Join Date: Jun 2005
Location: Singapore
Posts: 377
Thanks: 0
Thanked 1 Time in 1 Post
Quote:
Originally Posted by End User View Post
This may or may not be what you're looking for. The code will probably need to be modified for your application, but this could give you a starting point:

PHP Code: Weighted Random Choice - Random selection with a weighted bias.
Thanks. I have saw that before too. Well, I'll give it a try too. Will post back again.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[2005] StartPosition for CommonDialogs? tim8w Windows .NET Programming 10 01-08-09 03:39 AM
Parsing XML dodotopia PHP 1 02-08-06 03:51 PM
ASP upload prob minority ASP 1 06-27-05 08:35 AM
PHP Error Fairnie PHP 8 06-26-04 07:15 AM
Help trim code down TheLaughingBandit JavaScript 0 09-02-03 09:50 AM


All times are GMT -5. The time now is 08:35 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.