Function for random sorting of an array with weights
$10-30 USD
Igangværende
Slået op over 10 år siden
$10-30 USD
Betales ved levering
I need someone to create a random sort for an array that has weights.
I have an array with weights for example:
$videos = array (
"video1" => 25,
"video2" => 50,
"video3" => 75,
"video4" => 25,
"video5" => 50,
"video6" => 25,
"video7" => 50,
"video8" => 75,
"video9" => 25,
"video10" => 50
)
I want them to shuffle into a random order. I want it make all weights = 75 to show up towards the top of the sort MORE OFTEN than the 50. 50 more than 25. I DO NOT want all 75s before 50s, and all 50s before 25s.
I have something currently that is working well, but I fear it is not performing ideally like I want. It is this:
function wshuffle(&$array) {
foreach($array as $k=>$v) $tmp_array[$k] = mt_rand(0,$v);
arsort($tmp_array);
foreach($tmp_array as $k=>$v) $out_array[$k] = $array[$k];
$array = $out_array;
}
I will not accept your bid:
- If you are from India
- If you do not speak english well
- If you do not write well in english
- If you do not tell me how many stripes are in the american flag when you respond
I can give you this function. Can you confirm there are only 3 weights 75, 50 and 25? Or will there be many different possible weights? Do you just want the high weight values to be **predisposed** (higher chance) of appearing first? And what type of distribution model are you after (quadratic, linear etc?) If you have no idea what I'm talking about, then my bid is for handling any number of different possible weights, and for larger weight values to more often appear at the top of the list but NOT ALWAYS, and you don't have to worry about the intricate details. Reminder: no milestone, no start.
G'day Greg, I could give you a probabilistic function where the chance of an entry appearing next is directly proportional to its weight. It would include a parameter to specify how 'strong' you want the weighting to be factored so you could adjust the output to your liking. Just a quick question, how long will the arrays be? I don't know how many stripes are in the American flag but I think there are 52 stars.