lundi 14 septembre 2015

Can a random string be generated with the frequency of characters in it, fixed?

I am trying to generate random strings, with the number of occurence of each character,fixed. For example- G (a character in the string),cannot appear more than 4 times , S, no more than 2 times and so on. My code is able to generate the strings but not the problem with the frequency of characters.

Is there any way, to fix this thing ? Any help will really be appreciated.

My current code is as follows:

    #include<stdio.h>
    #include<stdlib.h>

    //The basic function here is like (rand() % range)+ Lower_bound
    int rand_int(int a,int b)
    {    
    if (a > b)  

        return((rand() % (a-b+1)) + b);    
    else if (b > a)        
        return((rand() % (b-a+1)) + a);    
    else        
       return a;
   } 
     int main()
 { 

   FILE *fptr;
   fptr=fopen("C:\\Users\\Desktop\\dataset.txt","w");    
   int a,b,r,i,j,k;    
   char res;    
    a = 65;    
    b = 90;    
    k = 0;    
    char str[] =     "ITLDGGCSSHLPLRCSVDSGCPGLRAGSVSCLPHGSIREGMECSRRHGVGIMGDRRDGSRDS";  //working string     
   char str_all[] = "ITLDGGCSSHLPLRCSVDSGCPGLRAGSVSCLPHGSIREGMECSRRHGVGIMGDRRDGSRDS";    
   char subs[] = "ACDEGHILMPRSTV";    //
   fprintf(fptr,"%s",str);
   fprintf(fptr,"%s","\n");

// value of j represents the number of strings to be displayed    
for(j=0 ; j<10000 ; j++)    
{      
     // getting all the sequence strings      
     // for changing the string at all positions      
      for(i=0 ; i<62 ; i++)      
      {            
         r = rand_int(a,b);        
         res = (char)r;        
         str_all[i] = res;      
      }

   // substituting all the not required characters with required ones      
      for(i=0 ; i<62 ; i++)      
      {      
         if(str_all[i] == 'B' || str_all[i] == 'F' || str_all[i] == 'J' || str_all[i] == 'K' || str_all[i] == 'N' || str_all[i] == 'O' || str_all[i] == 'Q' || str_all[i] == 'U' || str_all[i] == 'W' || str_all[i] == 'X' || str_all[i] == 'Y' || str_all[i] == 'Z')
         {      
           str_all[i] = subs[k++];      
         }      
         if(k>13)        
            k=0;   
      } 
      //printf("\nModified String for all string positions \n%s",str_all);  
        fprintf(fptr,"%s",str_all);
         fprintf(fptr,"%s","\n");
   }  
fclose(fptr);  
return 0;
}




Aucun commentaire:

Enregistrer un commentaire