产生随机序列的问题,向各位高手请教!
A、C、T、G四个字符随机产生500000长的字符序列,但要求A和C占随机序列的%60,这怎么弄呀?请教!谢谢!!
程序代码:#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<malloc.h>
int random(int max)
{
return ((rand() << 16)|((rand() & 1) << 15)|rand()) % max;
}
int main(int argc, char ** argv)
{
int n, a, c, t, g, i, j;
char * s, tmp;
if(argc != 5) return 0;
srand(time(NULL));
n = atoi(argv[1]);
if(n < 0) return 0;
a = (int)(atof(argv[2]) / 100 * n + 0.5);
if(a < 0) return 0;
c = (int)(atof(argv[3]) / 100 * n + 0.5);
if(c < 0) return 0;
t = (int)(atof(argv[4]) / 100 * n + 0.5);
if(t < 0) return 0;
g = n - a - c - t;
if(g < 0) return 0;
if(!(s = (char *)malloc(n + 1))) return 0;
for(i = 0; a--; s[i++] = 'A');
for(; c--; s[i++] = 'C');
for(; t--; s[i++] = 'T');
for(; g--; s[i++] = 'G');
s[n] = '\0';
for(i = 0; i < n; i++)
{
j = random(n);
tmp = s[i];
s[i] = s[j];
s[j] = tmp;
}
puts(s);
free(s);
return 0;
}

