[Face by] a face question about the stochastic algorithm

Source: Internet
Author: User

Today encountered an interview question: The original question is roughly, each song is a score, there are now 2000 songs, requires the implementation of a random player, each song playing probability should be proportional to its score, such as the score 9.1 songs, and 7.9 of the songs, the number of times should be 91:79.

The interviewer's answer is roughly the same:

First the score from small to large, then the score according to each song, generate a semi-closed interval, and then generate a random number, see random grumble in which interval, is the song chosen. For example, there are three songs, the score is [three-in-one] then it should be the generation of the third interval [0-1,1-3,3-6], then generate a random number between 0-6, randomly grumble in which interval, select the corresponding song. Consider the efficiency of sequencing, which is a nlogn algorithm.

However, this algorithm is flawed, did not take into account the repetition of the score, if the score of the three song is [1,2,2], then should be generated two intervals [0-1,1-5], if fall in the second interval, but also from the two of the songs divided into 2 of the song is randomly selected from the first. In this way, the implementation is quite complicated.

Finally, if you consider it as above, it will be complete, but if implemented, you will find that there is no good data structure to determine which random number falls in which interval, unless you traverse all the intervals.

So, elegant and efficient solution is what, assuming that each score only after the decimal point, then in fact, the use of space in exchange for the idea of time, just to each song according to his rating how many copies of the corresponding copy of the song, and all the songs are thrown into a pool inside, Then choose a song from the inside of the pool and wait for the probability. In the worst case, the score of 2000 songs is 9.9, then each song needs to replicate 99, space efficiency is on, time complexity is O1

The Scala implementation of the algorithm is as follows:

class Randomsong (val rate:array[double]) {  = rate.map (x = x). ToInt). Zipwithindex    case (rate, index) = + Array (index). Padto (rate, index)  }= Songpool (random.nextint ( songpool.size))}

Test

Object Main {  def main (args:array[string]) {    new Randomsong (Array (0.9,0.9,0.1,0.2))     = Map ()    1 to 10000      foreach {x = = R.picksong      count.get (song) match {  c12/> case None and Count + = (song-1        )case Some (n) = count + = (song (1 + N)}}    println ("count =" + count)   }}

Results

Count = Map (2-477, 1-4312, 3-970, 0-4241)

PS: I was on the way home to think of this solution, I and my wife said, the chemistry department graduated from her directly gave the right solution, hey, is the math genius the taste of the crush is so ecstasy.

[Polygon] a face question about a random algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.