(algorithm) Play songs randomly

Source: Internet
Author: User

Topic:

Assuming that there are 1000 songs in the mp3 of Zhang San, it is now desirable to design a random algorithm to play randomly. Unlike the ordinary random mode, Zhang San hope that each song is randomly drawn to the probability is proportional to a song of the watercress score (0~10 points), such as Hackberry's "ordinary Road" score of 8.9 points, the escape plan "the brightest star in the night" score of 9.5 points, then hope to listen to the "ordinary Road" probability and " The brightest star in the night sky is the probability ratio of 89:95. Now we know the 1000-song of the Watercress Score:
(1) Please design a stochastic algorithm to meet the requirements of Zhang San.
(2) Please write code to implement your own algorithm.

Ideas:

The 1000 Songs of the watercress score to become a 1000-interval axis, each interval represents a song, in each interval is evenly distributed, and interval and interval between the probability of meeting with the watercress score proportional.

In this way, by generating a random floating-point number between [0,totalscore], the song is played in which interval.

Code:
#include <iostream>#include<time.h>#include<stdlib.h>using namespacestd;intFindidx (DoubleSongs[],intNDoubleRnd) {    intleft=0; intright=n-1; intmid;  while(left<=Right ) {Mid= (left+right)/2; if((songs[mid-1]&LT;=RND) && (songs[mid]>=Rnd)) returnmid; if(songs[mid]>Rnd) Right=mid-1; Else Left=mid+1; }//return mid;}intRandomplaysong (DoubleSum_scores[],intN) {    Doublemx=sum_scores[n-1]; Doublernd= rand () *mx/(Double) (Rand_max); returnFindidx (SUM_SCORES,N,RND);}intMain () {Srand (0)); Doublescores[]={5.5,6.5,4.5,8.5,9.5,7.5,3.5,5.0,8.0,2.0}; intn=sizeof(scores)/sizeof(scores[0]); DoubleSum_scores[n]; sum_scores[0]=scores[0];  for(intI=1; i<n;i++) Sum_scores[i]=sum_scores[i-1]+Scores[i]; cout<<"Calculate The probability of each song:"<<Endl; inttotalscore=sum_scores[n-1];  for(intI=0; i<n;i++) cout<<scores[i]/totalScore<<" "; cout<<Endl; intCounts[n];  for(intI=0; i<n;i++) Counts[i]=0; intI=0; intidx; intMax_iter=100000000;  while(i<max_iter) {idx=Randomplaysong (sum_scores,n); COUNTS[IDX]++; I++; } cout<<"After simulation, probability of each song:"<<Endl;  for(intI=0; i<n;i++) cout<<1.0*counts[i]/max_iter<<" "; cout<<Endl; return 0;}
Operation Result:

The probability of each song based on the watercress score and the 100 million random test results can be seen in the Basic Agreement:

(algorithm) Play songs randomly

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.