CoderZone.org

Category: >> PHP Code >> Weighted Random Number Function Bookmark and Share

<< lastnext >>

Snippet Name: Weighted Random Number Function

Description: Randomly generates a weighted integer between two other (specified) integers (which can include them). Can be used for getting random indexes from an array, where the lower the index, the higher the chances of it being selected. Passing a value of 1 will give all numbers in the range an equal chance of being selected. A value of 2 should be sufficient in most instances. If you want high numbers to occur less often, use higher values.

Also see:
» Generate Random Keys
» Exponentially Weighted Random Numb...
» Random Image from Directory
» Random banner picker
» Generate random string
» Rounded random numbers
» Random Pronounceable Passwords
» Pick Randomly from Array
» Weighted Random Choice

Comment: (none)

Author: CoderZone
Language: PHP
Highlight Mode: PHP
Last Modified: December 02nd, 2010

function weighted_rand($min, $max, $factor){
	return round($min + (pow(rand(0, $max) / $max, $factor) * ($max - $min)));
}
 
There haven't been any comments added for this snippet yet. You may add one if you like.  Add a comment 
© coderzone.org | users online: 21