[C ++] Rename an MP3 file at random

Source: Internet
Author: User

A new MP3 file has no random playback function! As a programmer, do it yourself ~

Obtain the current path:

char buf[1000];GetCurrentDirectory(1000,buf); string cur_folder_file = string(buf)+"\\*.mp3";
Obtain the list of mp3 files in the current path:

void getMp3Names(string filepath,vector<string> &names){names.clear();WIN32_FIND_DATA f;HANDLE h = FindFirstFile(filepath.c_str() , &f);if(h != INVALID_HANDLE_VALUE){do{names.push_back(f.cFileName);} while(FindNextFile(h, &f));}FindClose(h);}
Random rename mp3file (Name: num.mp3, for example, 5.mp3 ):

void renameMp3Names(vector<string> &names){int mp3_num = names.size();vector<bool> used_names(mp3_num,false);srand(time(NULL));  for(int i=0;i<mp3_num;i++){int new_name_num = -1;while(new_name_num<0){int tmp= rand()%mp3_num;if(!used_names[tmp]){new_name_num = tmp;used_names[tmp] = true;}}char char_tmp[100];itoa(new_name_num,char_tmp,10);string new_name = string(char_tmp)+".mp3";if(!_access(names[i].c_str(),0)){if(!rename(names[i].c_str(),new_name.c_str())){cout<<"RENAME "<<names[i]<<" AS: "<<new_name<<""<<endl;}}}return ;}

Okay ~


However, this program runs for the second time. Because all the files are changed to numparts, conflicts will occur when you rename them randomly again, and rename () will fail because of the existence of duplicate files!

Make a small modification:

void renameMp3Names(vector<string> &names){cout<<"---------------RENAME MP3 FILES---------------"<<endl;int mp3_num = names.size();vector<bool> used_names(mp3_num,false);srand(time(NULL));  for(int i=0;i<mp3_num;i++){int new_name_num = -1;while(new_name_num<0){int tmp= rand()%mp3_num;if(!used_names[tmp]){new_name_num = tmp;used_names[tmp] = true;}}char char_tmp[100];itoa(new_name_num,char_tmp,10);string new_name = string("tmp")+string(char_tmp)+".mp3";if(!_access(names[i].c_str(),0)){if(!rename(names[i].c_str(),new_name.c_str())){cout<<"RENAME "<<names[i]<<" AS: "<<new_name<<""<<endl;names[i] = new_name;//used_names[i] =false;}}}for(int i=0;i<mp3_num;i++)used_names[i] = false;cout<<endl;cout<<"-------------AGAIN RENAME MP3 FILES-------------"<<endl;for(int i=0;i<mp3_num;i++){int new_name_num = -1;while(new_name_num<0){int tmp= rand()%mp3_num;if(!used_names[tmp]){new_name_num = tmp;used_names[tmp] = true;}}char char_tmp[100];itoa(new_name_num,char_tmp,10);string new_name = string(char_tmp)+".mp3";if(!_access(names[i].c_str(),0)){if(!rename(names[i].c_str(),new_name.c_str())){cout<<"RENAME "<<names[i]<<" AS: "<<new_name<<""<<endl;}}}cout<<endl;return ;}



Success!

* Modifications to generate random sequences

Thanks to the comments from xiucaijiang on the first floor, it is pointed out that the efficiency of the above method to generate random sequences is very low. Indeed, the method used above is to initialize a sequence to record the "used" number. If the generated random number is a used number, it will be regenerated again. For a random sequence with N size, the expected number of times of generating the first random number is 1 and the second is N/(N-1, the third time is N/(N-2) times, and the last expected number of times is N times, the efficiency is indeed very low. Xiucaijiang provides a good method:


We can understand this method as follows: generating a random sequence is equivalent to putting the previous 0 ~ The N number of N-1 sorting is randomly arranged, so each time we randomly extract one to the back. That is, for the first time in all the N number of random pumping to the last bit, the second in the previous number of random N-1 to the last second, and so on. Because the probability of each number being drawn in each sort is 1/N, the final sequence can be considered as a random sorting.

However, the implementation of this method requires several unnecessary shifts. For example, in the above example, the second hypothesis is 2, which is changed from 0, 1, 2, 5, 4 to 0, 1, 3, 5, 2, 4: Move 3 to the third place, 5 to the third place, and 2 to the fourth place. Here the shift of 3 and 5 is actually unnecessary. That is to say, the next time we randomly take the four numbers, the first four numbers are arranged as, and 3. Therefore, you only need to change the positions of 2 and 5.

The method for generating a random sequence is summarized as follows: round N times, I (I = 0 ~ N-1) operation is to randomly select a number in the first N-I number and the number of N-i-1 switching position.

void generateRandomList(vector<int> & rand_list){int rand_size = rand_list.size();for(int i=0;i<rand_size;i++){rand_list[i] = i;}srand(time(NULL)); for(int i=0;i<rand_size;i++){int tmp_rand= rand()%(rand_size-i);int tmp = rand_list[tmp_rand];rand_list[tmp_rand] = rand_list[rand_size-1-i];rand_list[rand_size-1-i] = tmp;}return;}

This time, we only need to randomly generate random sequences that are not repeated N times ~

(Reprinted please indicate the author and Source: http://blog.csdn.net/xiaowei_cqu is not allowed for commercial use)


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.