Is there a way to make the rand function some more selective ? here's what i want to do imagine rand(0, 2000) .. it will return equally probable values from that range, right? so now i want it to return more probable values near 2000 then 0 .. in other words, i want to increase the probability near the max limit of the rand is there such a function to do that ? Thanks
Hmm, that's a tough one. rand() and the more random mt_rand() both only accept a min and max argument. You could try something like this: $rand_adjust = rand(0,100); if($rand_adjust <= 50) $random = rand(0,2000); else $random = rand(1000,2000); PHP: So that way, you are controlling the probability in favor of higher values. You can obviously add to that and customize it to change the probability, but other than that, you can't affect the probability of a random number.
$ranges = array(0, 1000, 1500); $rand = rand($ranges[array_rand($ranges)], 2000); echo $rand; PHP: This does the trick.
That'll also work, and is admittedly a cleaner way of doing it, allowing you to store all the different adjustments in a single array. So for something like that: 1/3 chance of 0-2000 1/3 chance of 1000-2000 1/3 chance of 1500-2000 You'll end up with 0-1000: (1/3 * 1/2 * 1/1000) = 1/6000 chance of each # 1000-1500: ((1/3 * 1/4 * 1/500) + (1/3 * 1/2 * 1/500)) = 1/2000 chance of each # 1500-2000: ((1/3 * 1/4 * 1/500) + (1/3 * 1/2 * 1/500) + (1/3 * 1 * 1/500)) = 35/30000 chance of each # Or 0-1000 : 16.6% chance 1000-1500: 25% chance 1500-2000: 58.3% chance Now you can see, it's fairly easy to customize the rand to suit your needs.