tags:

views:

168

answers:

4

i would like to know what algorithm and what programming language wikipedia is using to randomly choose an article to display.

i would also like to know how does it work so fast?

+2  A: 

From MediaWiki.org:

MediaWiki is a free software wiki package written in PHP, originally for use on Wikipedia. It is now used by several other projects of the non-profit Wikimedia Foundation and by many other wikis, including this website, the home of MediaWiki.

MediaWiki is open source, so you can download the code and inspect it, to see how they have implemented this feature.

Stephan202
+5  A: 

Here's information on that.

Every article is assigned a random number between 0 and 1 when it is created (these are indexed in SQL, which is what makes selection fast). When you click random article it generates a target random number and then returns the article whose recorded random number is closest to this target.

If you are interested you can read the actual code here.

how does it return the article whose recorded random number is closest to this target? do you think it sorts the entire table first?
I__
I guess the table is sorted, yes. This way, you can insert and find items very fast (should be O(log(N)) using binary search.
schnaader
If it's sorted the sorted table will be kept and new articles will be integrated into it somehow.
+3  A: 

Something along this lines:

 "SELECT cur_id,cur_title
        FROM cur USE INDEX (cur_random)
        WHERE cur_namespace=0 AND cur_is_redirect=0
        AND cur_random>RAND()
        ORDER BY cur_random
        LIMIT 1"
SilentGhost
+1  A: 

If you look at the source, they use PHP/MySQL a sort and filter pages by pregenerated random values (page_random column) that have an index on them.

che