<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>20bits - Latest Comments in Random Weighted Elements in PHP</title><link>http://20bits.disqus.com/</link><description></description><atom:link href="https://20bits.disqus.com/random_weighted_elements_in_php/latest.rss" rel="self"></atom:link><language>en</language><lastBuildDate>Sun, 06 Jun 2010 23:29:03 -0000</lastBuildDate><item><title>Re: Random Weighted Elements in PHP</title><link>http://20bits.com/articles/random-weighted-elements-in-php/#comment-55076623</link><description>&lt;p&gt;Excellent, exactly what I needed.  Doing a genetic algorithm AI and need to pick a "gene" (candidate solution) weighted according to its score or suitability to solving the problem at hand.&lt;br&gt;The prize, of course, is the opportunity to mate with another gene from the pool. :)&lt;br&gt;Very sexy stuff we're dealing with here.&lt;/p&gt;&lt;p&gt;Thanks&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Lance</dc:creator><pubDate>Sun, 06 Jun 2010 23:29:03 -0000</pubDate></item><item><title>Re: Random Weighted Elements in PHP</title><link>http://20bits.com/articles/random-weighted-elements-in-php/#comment-52224901</link><description>&lt;p&gt;very concise and accurate. Well done&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">data recovery</dc:creator><pubDate>Wed, 26 May 2010 09:18:10 -0000</pubDate></item><item><title>Re: Random Weighted Elements in PHP</title><link>http://20bits.com/articles/random-weighted-elements-in-php/#comment-47590378</link><description>&lt;p&gt;I found a much simpler way about this : &lt;a href="http://www.danmorgan.net/programming/weighted-random-and-weighted-shuffle/" rel="nofollow noopener" target="_blank" title="http://www.danmorgan.net/programming/weighted-random-and-weighted-shuffle/"&gt;http://www.danmorgan.net/pr...&lt;/a&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Dan Morgan</dc:creator><pubDate>Fri, 30 Apr 2010 00:42:04 -0000</pubDate></item><item><title>Re: Random Weighted Elements in PHP</title><link>http://20bits.com/articles/random-weighted-elements-in-php/#comment-45188410</link><description>&lt;p&gt;As far as I know in general case this can be only done via some sort of approximation for float random number. For integers in fixed space it's pretty easy task: &lt;br&gt;For example I have some random distribution where "1" probability is two times less then "2", "2" two time less then "3" and so on to max of "10".&lt;br&gt;I fill an array&lt;br&gt;[probablity of "1", probabilty of ("1" or "2"),...]&lt;br&gt;then give some number (just to have numbers) to each probability. Numbers will be relative. For simplicity let's say that probablity of "1" is equal to one:&lt;br&gt;[1,1+2,1+2+4, ..,1+2+4+..+..512] =&lt;br&gt;[1,3,7,...,1023]&lt;br&gt;Then use such array as a random map by generating random number between 1 and 1023. i.e in case i = rnd(1,1023) = 5 it maps to 3th cell of random map (3 &amp;lt; 5 &amp;lt;= 7) and leads to number "3" in the result.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Meta</dc:creator><pubDate>Fri, 16 Apr 2010 14:45:01 -0000</pubDate></item><item><title>Re: Random Weighted Elements in PHP</title><link>http://20bits.com/articles/random-weighted-elements-in-php/#comment-24123742</link><description>&lt;p&gt;Oh! just realised:&lt;br&gt;4. it should be $r = mt_rand(0,1000) or mt_rand(0,$factor) [NOT mt_rand(1,1000)] as the whole affair is merely a scaling of mt_rand(0,1)&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Andy</dc:creator><pubDate>Thu, 26 Nov 2009 06:11:06 -0000</pubDate></item><item><title>Re: Random Weighted Elements in PHP</title><link>http://20bits.com/articles/random-weighted-elements-in-php/#comment-24123553</link><description>&lt;p&gt;Thanks for this. &lt;br&gt;A few thoughts, in response to this and comments:&lt;br&gt;1. Yes, the 1000 factor is arbitrary and should be replaced if not approp to your context.&lt;br&gt;2. if the same weights are to be used repeatedly in a session, it may be worth setting up  array of factored cumulative vals from the original weighted. This makes the loop a straight lookup rather than doing the same cumulative calcs each time.&lt;br&gt;3. I keep thinking about the inefficiency of the 'bottom to top' comparison order. I wonder how much difference this makes with a large number of elements?&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Andy</dc:creator><pubDate>Thu, 26 Nov 2009 05:59:59 -0000</pubDate></item><item><title>Re: Random Weighted Elements in PHP</title><link>http://20bits.com/articles/random-weighted-elements-in-php/#comment-23605875</link><description>&lt;p&gt;Add sorting to further increase accuracy, but at a cost of speed. (not alot though!)&lt;/p&gt;&lt;p&gt;arsort($weights,SORT_NUMERIC);&lt;br&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">crhatfield</dc:creator><pubDate>Fri, 20 Nov 2009 03:27:44 -0000</pubDate></item><item><title>Re: Random Weighted Elements in PHP</title><link>http://20bits.com/articles/random-weighted-elements-in-php/#comment-23473087</link><description>&lt;p&gt;Konrad, your implementation is not accurate.  see for yourself.  accumulate 100,000 cycles of a 50/50 weight. you will always get A around 49.5 and B around 50.5.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">leeoniya</dc:creator><pubDate>Wed, 18 Nov 2009 14:02:47 -0000</pubDate></item><item><title>Re: Random Weighted Elements in PHP</title><link>http://20bits.com/articles/random-weighted-elements-in-php/#comment-12996934</link><description>&lt;p&gt;There are some improvements I noticed when I converted this to Python.&lt;br&gt;First instead of adding onto the extra variable offset you could just substract from r.&lt;br&gt;Also replacing 1000 by the sum of all weights makes things more flexible cause you can easily go by just having counts instead of weights also you don't have to make the sum up to 1. And even leave out the multiplication in line 5.&lt;br&gt;This is what I have implemented in Python&lt;/p&gt;&lt;p&gt;def wchoice(lst):&lt;br&gt;    wtotal = sum(lst)&lt;br&gt;    n = random.uniform(0, wtotal)&lt;/p&gt;&lt;p&gt;    for i, weight in enumerate(lst):&lt;br&gt;        if n &amp;lt; weight:&lt;br&gt;            break&lt;br&gt;        n = n - weight&lt;br&gt;    return i&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Konrad</dc:creator><pubDate>Tue, 21 Jul 2009 10:20:48 -0000</pubDate></item><item><title>Re: Random Weighted Elements in PHP</title><link>http://20bits.com/articles/random-weighted-elements-in-php/#comment-9799830</link><description>&lt;p&gt;As far as I can tell, the choice of 1000 within the function is fairly arbitrary.&lt;/p&gt;&lt;p&gt;I replaced it with 10, 100, 10000, and 42 and got seemingly equally random results (just eyeballing them).&lt;/p&gt;&lt;p&gt;I was careful, however, to provide an array with weights between 0 and 1, and whose sum was 1.&lt;br&gt;I.e., $ws = array(array(0.1,1),array(0.2,2),array(0.3,3),array(0.4,4));&lt;/p&gt;&lt;p&gt;But I think you'll be in trouble no matter what if your array input does not give a full 100% values.&lt;/p&gt;&lt;p&gt;I'd have to test more to be sure, but it's easier to just validate the input. :-)&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Richard Lynch</dc:creator><pubDate>Fri, 22 May 2009 13:06:37 -0000</pubDate></item><item><title>Re: Random Weighted Elements in PHP</title><link>http://20bits.com/articles/random-weighted-elements-in-php/#comment-8633884</link><description>&lt;p&gt;This is great, looking for something similar, the only thing that can't quite work on my side, is what if i have about 1,000 elements and I want certain ones to have more weight than others?&lt;/p&gt;&lt;p&gt;I would have to write &lt;br&gt;A = [1,2-999,1000]&lt;/p&gt;&lt;p&gt;but what about the weight? it would be kind of hard I guess to divide 100 / 1,000 and just give weight to certain items...&lt;/p&gt;&lt;p&gt;any solution for this kind of problem?&lt;/p&gt;&lt;p&gt;something like: in my sql i have 1,000 items, in the table i have 3 rows, ID, NAME, WEIGHT  then the items that have a higher number (1,2,3,4 ...) have more chance of showing up, 2 has double the chance of 1 and 3 double the chance of 2 but triple the chance of 1  and so on...&lt;/p&gt;&lt;p&gt;any idea?&lt;/p&gt;&lt;p&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">niabi</dc:creator><pubDate>Thu, 23 Apr 2009 20:58:03 -0000</pubDate></item><item><title>Re: Random Weighted Elements in PHP</title><link>http://20bits.com/articles/random-weighted-elements-in-php/#comment-7353755</link><description>&lt;p&gt;for anyone wondering... you need to adjust the random range and the 1000 multiplier according to your weights&lt;/p&gt;&lt;p&gt;ie the min and max rand value should (I think) correspond to the smallest possible weight value and the sum of all your weight values respectively (?)&lt;/p&gt;&lt;p&gt;and if you use a multiplier that would be...&lt;/p&gt;&lt;p&gt;min rand value = smallest possible weight * multiplier&lt;br&gt;max rand value = sum of all weight values in set * multiplier&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">blueskiwi</dc:creator><pubDate>Thu, 19 Mar 2009 14:45:16 -0000</pubDate></item><item><title>Re: Random Weighted Elements in PHP</title><link>http://20bits.com/articles/random-weighted-elements-in-php/#comment-7353489</link><description>&lt;p&gt;awesome, you rock, this is just what I needed, thanks!!&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">blueskiwi</dc:creator><pubDate>Thu, 19 Mar 2009 14:35:08 -0000</pubDate></item><item><title>Re: Random Weighted Elements in PHP</title><link>http://20bits.com/articles/random-weighted-elements-in-php/#comment-6256097</link><description>&lt;p&gt;Thank you!  Exactly what I was looking for.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Matt</dc:creator><pubDate>Sat, 14 Feb 2009 02:04:38 -0000</pubDate></item><item><title>Re: Random Weighted Elements in PHP</title><link>http://20bits.com/articles/random-weighted-elements-in-php/#comment-6205714</link><description>&lt;p&gt;This is great - a nice, tight function that does exactly what I was looking for - with one possible exception.  Does this work well if you are drawing from an array with, say, 200 elements?  Does adjusting the weight to the thousandths instead of the hundredths result with any degree of accuracy?&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Porkins</dc:creator><pubDate>Thu, 12 Feb 2009 09:08:40 -0000</pubDate></item><item><title>Re: Random Weighted Elements in PHP</title><link>http://20bits.com/articles/random-weighted-elements-in-php/#comment-5512147</link><description>&lt;p&gt;I've been looking for something which does exactly this for a very long time now, and this is by far more elegant than anything I've been able to make so far. Thanks!&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Ishaan</dc:creator><pubDate>Sat, 24 Jan 2009 01:35:42 -0000</pubDate></item><item><title>Re: Random Weighted Elements in PHP</title><link>http://20bits.com/articles/random-weighted-elements-in-php/#comment-4424991</link><description>&lt;p&gt;I'm going to implement this for my python program, thanks!&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">pj</dc:creator><pubDate>Tue, 16 Dec 2008 01:18:56 -0000</pubDate></item><item><title>Re: Random Weighted Elements in PHP</title><link>http://20bits.com/articles/random-weighted-elements-in-php/#comment-3793427</link><description>&lt;p&gt;Thank you very much for this awesome trick, i was working on a small application, and i was using the array method, but i didn't like it, as you said it takes lots of resources if there was a big set of data, which is the case for me.&lt;br&gt;Wish you the best.&lt;br&gt;Regards.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Kareem</dc:creator><pubDate>Tue, 09 Sep 2008 20:00:44 -0000</pubDate></item><item><title>Re: Random Weighted Elements in PHP</title><link>http://20bits.com/articles/random-weighted-elements-in-php/#comment-3793428</link><description>&lt;p&gt;Hmmm.  I like your function, but here's what I'm looking to do...&lt;/p&gt;&lt;p&gt;I want to generate a random number from 0 to 6000.  I want their to be the highest probability (40% chance) of getting between 100 and 2000, inclusive.  I want the probability of getting a number from 99 to 0, to gradually decrease, so that it would be almost impossible to get 0.  Just to be clear the probability of getting 98 should be less than that of 99, the probability of getting 97 should be less than the probability of getting 98, etc. Similarly, I want the probability of getting a number from 2001 to 6000 (again including 2001 and 6000) to gradually decrease, so that it's almost impossible to get 6000.&lt;/p&gt;&lt;p&gt;Anyone know how I could do this?&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Venkat Koduru</dc:creator><pubDate>Sun, 11 May 2008 08:18:19 -0000</pubDate></item><item><title>Re: Random Weighted Elements in PHP</title><link>http://20bits.com/articles/random-weighted-elements-in-php/#comment-3793426</link><description>&lt;p&gt;Brilliant, and superbly clean code and explanation!&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Jeremy</dc:creator><pubDate>Sat, 26 Apr 2008 18:59:03 -0000</pubDate></item><item><title>Re: Random Weighted Elements in PHP</title><link>http://20bits.com/articles/random-weighted-elements-in-php/#comment-3793424</link><description>&lt;p&gt;Mike,&lt;/p&gt;&lt;p&gt;X = f(W) is the random variable in question, if you want to make it even more formal.  The specific properties of X, e.g., its CDF, are determined entirely by W.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Jesse</dc:creator><pubDate>Thu, 07 Feb 2008 19:18:58 -0000</pubDate></item><item><title>Re: Random Weighted Elements in PHP</title><link>http://20bits.com/articles/random-weighted-elements-in-php/#comment-3793425</link><description>&lt;p&gt;Great post. Now I'm trying to remember what I learned in my courses on probability in college... seems like there is a probability distribution for this, or something like that... what's the difference between a PDF and a CDF again? In any case, I think your method is essentially the same thing a stats class would have you do. :P&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Mike</dc:creator><pubDate>Thu, 07 Feb 2008 19:13:20 -0000</pubDate></item><item><title>Re: Random Weighted Elements in PHP</title><link>http://20bits.com/articles/random-weighted-elements-in-php/#comment-3793422</link><description>&lt;p&gt;Chris,&lt;/p&gt;&lt;p&gt;Yeah, I'm trying to get back into it.  Ever since I sold &lt;a href="http://adonomics.com" rel="nofollow noopener" target="_blank" title="http://adonomics.com"&gt;Adonomics&lt;/a&gt; I've been working more than full-time on it.  Between that and a desire for an active social life I don't have much time for blogging.&lt;/p&gt;&lt;p&gt;I'm going to try to write one article at least once every other week, though.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Jesse</dc:creator><pubDate>Thu, 07 Feb 2008 17:33:32 -0000</pubDate></item><item><title>Re: Random Weighted Elements in PHP</title><link>http://20bits.com/articles/random-weighted-elements-in-php/#comment-3793423</link><description>&lt;p&gt;I'm glad you're still blogging.  It's been a while since your previous post.  I've enjoyed reading your posts on programming (especially the one on MySQL improvements).  Thanks for writing these useful posts.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Chris</dc:creator><pubDate>Thu, 07 Feb 2008 17:26:13 -0000</pubDate></item></channel></rss>