Reputation: 10012
So I have the GDP for the top 50 countries in the world:
USA,16800000,1
CHN,9240270,2
JPN,4901530,3
DEU,3634823,4
FRA,2734949,5
GBR,2521381,6
BRA,2245673,7
RUS,2096777,8
ITA,2071307,9
IND,1876797,10
CAN,1826769,11
AUS,1560597,12
ESP,1358263,13
KOR,1304554,14
MEX,1260915,15
IDN,868346,16
TUR,820207,17
NLD,800173,18
SAU,745273,19
CHE,650377,20
ARG,611755,21
SWE,558949,22
NGA,521803,23
POL,517543,24
NOR,512580,25
BEL,508116,26
VEN,438284,27
AUT,415672,28
THA,387252,29
ARE,383799,30
COL,378148,31
IRN,368904,32
ZAF,350630,33
DNK,330614,34
MYS,312435,35
SGP,297941,36
ISR,291357,37
CHL,277199,38
HKG,274013,39
PHL,272017,40
EGY,271973,41
FIN,256842,42
GRC,241721,43
PAK,236625,44
KAZ,224415,45
IRQ,222879,46
PRT,220022,47
IRL,217816,48
DZA,210183,49
QAT,202450,50
Format: ISO code, GDP, rank
Source: World Bank
I would like to analyse every country on this list every 60 seconds. But I can't - the service I'm using (Twitter API) is rate limited.
So, I will randomly select 15 of the 50 countries, with the countries with the most GDP being weighted favourably, and the countries with the least GDP being weighted less so.
I'm using this function ($values and $weights are the 1st and 2nd columns in the above data - parsing not shown)
/**
* getSample()
* Pick a random item based on weights.
*
* @param array $values Array of elements to choose from
* @param array $weights An array of weights. Weight must be a positive number.
* @return mixed Selected element.
* http://stackoverflow.com/questions/445235/generating-random-results-by-weight-in-php
*/
function getSample($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];
}
I'm getting this kind of output from 100 calls:
CHN GBR ITA USA ESP MEX ZAF CAN JPN ITA COL USA USA FRA USA CHN USA IND ESP MEX CHN JPN USA USA CAN DEU USA USA USA JPN NLD CHN USA USA FRA USA TUR GBR CHN BRA USA BEL JPN USA TUR RUS DEU USA THA USA USA DEU AUS CHL CHN MEX USA USA CHN PRT SAU ITA IND USA RUS IND AUS ESP USA KOR CHN USA JPN USA IDN USA CHN FIN USA JPN PRT USA AUS USA JPN USA USA USA CHN JPN THA CHN TUR CHN FRA USA USA MEX GBR CHN
USA and CHN are coming up too often!
Is there a way to tweak this?
Can anyone point me in the right direction?
I'm coding all this in PHP.
Upvotes: 1
Views: 177
Reputation: 737
the easiest way is to dynamically adjust the weight so for instance take the initial weight and multiply it by the number iterations of the calls since that country has been called. then simply sort the list by wieghted order. So for instance the USA will be moved below smaller GDP countries based on how long those countries have been waiting in the queue
Upvotes: 1