Quick Search:
CODE
Oracle PL/SQL Code Library
JOBS
Find Or Post Oracle Jobs
FORUM
Oracle Discussion & Chat
 PHP Code: Weighted Random Choice Jump to:  
Category: >> PHP Code >> Weighted Random Choice Bookmark and Share

<< lastnext >>

Snippet Name: Weighted Random Choice

Description: Random selection with a weighted bias.

Also see:
» Random Image from Directory
» Random banner picker
» Generate random string
» Rounded random numbers
» Random Pronounceable Passwords
» Pick Randomly from Array

Comment: (none)

Language: PHP
Highlight Mode: PHP
Last Modified: March 16th, 2009

<?PHP
 
// Give higher priorities to the users who pay the most.
// Gives lowest-priced rows the highest priority:
 
SELECT * FROM LINK ORDER BY RAND()*payPerClick/RAND();
 
 
// Gives highest-priced rows the highest priority
 
SELECT 'field' FROM 'table' WHERE 'ppc' > 0 ORDER BY RAND() / 'ppc' * RAND()
 
// Running the query above 6000 times produces:
// 3 cent: 402
// 4 cent: 517
// 5 cent: 615
// 7 cent: 800
// 9 cent: 1020
// 11 cent: 1214
// 13 cent: 1432
 
 
// OR
 
FUNCTION w_rand($weights) {
    $r = MT_RAND(1,1000);
    $offset = 0;
    FOREACH ($weights AS $k => $w) {
        $offset += $w*1000;
        IF ($r <= $offset) {
            RETURN $k;
        }
    }
} 
 
?>


Free
Oracle Magazine
Subscriptions
and Oracle White Papers


SQL University.net courses meet the most demanding needs of the business world for advanced education in a cost-effective manner. SQL University.net courses are available immediately for IT professionals and can be taken without disruption of your workplace schedule or processes.

Compared to traditional travel-based training, SQL University.net saves time and valuable corporate resources, allowing companies to do more with less. That's our mission, and that's what we deliver.

Click here to find out more
 
Home      :      Code Library      :      Sponsors      :      Privacy      :      Terms of Use      :      Contact Us 305 users online    © 2009 psoug.org

PSOUG LOGIN
Username: 
Password: 
Forgot your password?