google 线性同余 伪随机数

解决方案 »

  1.   


    #include<stdio.h>
    #include<string.h>
    #include<math.h>#define MAX_SIZE 255void removeChar(char* str,int index);
    char* getFileIDMixString(double seed);char* getFileIDMixString(double seed)
    {
    char mixed[MAX_SIZE];
    memset(mixed,0,sizeof(mixed));
    char source[]="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/\\:._-1234567890";
    int index=0;
    int len=strlen(source);
    for(int i=0,j=0;i<len;i++)
    {
    seed=(int)(seed*211+30031)%65536;
    index=(int)floor(seed/65536*strlen(source));
    mixed[j++]=source[index];
    removeChar(source,index);
    }
    return mixed;
    }void removeChar(char* str,int index)
    {
    int i,j;
    for(i=0;str[i]!='\0';i++)
    {
    if(i==index)
    {
    for(j=i+1;str[j]!='\0';j++)
    {
    str[j-1]=str[j];
    }
    str[j-1]='\0';
    break;
    }
    }
    }
      

  2.   


    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>#define MAX_SIZE 255
     
    int splitString(char* str,char** arr,const char* delim);
    void removeChar(char* str,int index);
    char* getFileIDMixString(double seed);
    char* getFileID(const char* fileid, double seed);int splitString(char* str,char** arr,const char* delim)
    {
    if(str==NULL) return 0;
    int i=0;
    char* p=strtok(str,delim);
    while(p!=NULL)
    {
    arr[i++]=p;
    p=strtok(NULL,delim);
    }
    return i;
    }void removeChar(char* str,int index)
    {
    if(str==NULL) return;
    int len=strlen(str);
    if(index<0||index>len-1) return;
    if(index==len-1)
    {
    str[index]='\0';
    return;
    }
    int i;
    for(i=index+1;str[i]!='\0';i++)
    {
    str[i-1]=str[i];
    }
    str[i-1]='\0';
    }char* getFileIDMixString(double seed)
    {
    char* mixed=(char*)malloc(sizeof(char)*MAX_SIZE);
        char source[]="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/\\:._-1234567890";
        int index=0;
        int len=strlen(source);
    int i,j;
        for(i=0,j=0;i<len;i++)
        {
            seed=(int)(seed*211+30031)%65536;
            index=(int)floor(seed/65536*strlen(source));
            mixed[j++]=source[index];
            removeChar(source,index);
        }
    mixed[j++]='\0';
        return mixed;
    }char* getFileID(char* fileid,double seed)
    {
    char* realId=(char*)malloc(sizeof(char)*MAX_SIZE);
    char* mixed=getFileIDMixString(seed);
    char* ids[MAX_SIZE];
    int n=splitString(fileid,ids,"*");
    int idx,i,j;
    for(i=0,j=0;i<n-1;i++)
    {
    idx=atoi(ids[i]);
    realId[j++]=mixed[idx];
    }
    realId[j++]='\0';
    return realId;
    }int main()
    {
    char ss[]="7*8*9";
    printf("%s\n",getFileID(ss,8));
    return 0;
    }