#include "stdio.h"
#include "stdlib.h"
#define RANDOM_MAX 2147483647
#define MAX 100
//#define MAX 10
int prob(double x);
int main ()
{
//double mu = 0.0009/1000;
double mu = 0.0009/10;
int i;
for(i=0; i < MAX; i++){
printf("%d:", i);
if(prob(mu) == 1) printf("1\n");
else printf("0\n");
}
return 0;
}
int prob(double x)
{
double r;
random(); // 念のため一度フラッシュ.
r=random()/(RANDOM_MAX+1.0); // 「+1」だとマイナスの値になる.「2 の補数」.
//printf("random: %ld\t", random());
//printf("r:%e | ", r);
if (r < x) return 1;
return 0;
}
}
|
#include "stdio.h"
#include "stdlib.h"
#define NOTHING -1
struct chromosome
{
int name;
int *genes;
int length;
struct chromosome *next_chromosome;
} *first_chromosome;
int chr_num = 5;
int gene_len = 3;
struct chromosome *new_chromosome(int chrN, int len)
{
struct chromosome *chp;
int i;
if (chrN<0)
{
fprintf(stderr,"Negative chrN\n");
exit(1);
}
if (len<0)
{
fprintf(stderr,"Negative length\n");
exit(1);
}
chp = (struct chromosome*)malloc(sizeof(struct chromosome));
if (chp==(struct chromosome*)NULL)
{
fprintf(stderr,
"Could not allocate memory for new chromosome\n");
exit(1);
}
chp->genes = (int*)malloc(sizeof(int)*(len+1));
if (chp->genes==NULL)
{
fprintf(stderr,
"Could not allocate memory for new chromosome's genes\n");
exit(1);
}
for (i=0; i<=len; i++) chp->genes[i] = i;
chp->name = chrN;
chp->length = len;
if (first_chromosome == (struct chromosome*)NULL)
{
chp->next_chromosome = (struct chromosome*)NULL;
}
else
{
chp->next_chromosome = first_chromosome;
}
first_chromosome = chp;
return chp;
}
int main()
{
struct chromosome *chp;
int i;
for (i=0; i< chr_num; i++)
{
new_chromosome(i, gene_len);
}
// PRINTOUT
for (chp=first_chromosome;
chp!=(struct chromosome*)NULL;
chp=chp->next_chromosome)
{
printf("Chr%d; length:%d \n", chp->name, chp->length);
for (i=0; i < gene_len; i++)
printf(" genes[%d]:%d\n", i, chp->genes[i]);
}
return 0;
}
|