I cant seem to figure out whats wrong with this code, I am trying to generate random floating point number numbers with values from 0 to 1 with positive and negative values

here is the code

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

void populate(int generations)
{

int seed, ttt;
seed = 456739853;
  srand48(seed);


int mom_gene[generations];
int dad_gene[generations];

float y;
y= (drand48()/10.0);
//float x;
//x=(y/10.0);

   printf("\n Here are the values of rand and y, %f, %f", drand48(), y);
   printf("\n");
//  printf("time\tpopSize\n");
  for (ttt = 0; ttt < generations; ttt++)
    {
	if(drand48()>0.50)
	{ mom_gene[ttt]=drand48();}
        else
        { mom_gene[ttt]=-drand48();}
  //    printf("\n %f", mom_gene[ttt]);
    }

  for (ttt = generations; ttt > 0; ttt--)
    {
     if(drand48()>0.50)
	{ dad_gene[ttt]=drand48();}
        else
        { dad_gene[ttt]=-drand48();}
 //    printf("\n%f", dad_gene[ttt]);
     }

printf("\n");
  for (ttt = 0; ttt < generations; ttt++)
{    printf("\n%f", dad_gene[ttt]);
    printf("\n%f", mom_gene[ttt]);
}

return;
}




main()
{

//int seed = 456739853;
//  srand48(time(NULL));


populate(10);
}

Recommended Answers

All 2 Replies

I think I found my bug
here is the fixed code

#include <stdio.h>
#include <stdlib.h>
// int ttt;

void populate(int generations)
{

int seed, ttt;
seed = 456739853;
  srand48(seed);

double r;
double mom_gene[generations];
double dad_gene[generations];

//float y;
//y= (drand48()/10.0);
//float x;
//x=(y/10.0);

   printf("\n Here are the values of rand and y, %f", drand48());
   printf("\n");
//  printf("time\tpopSize\n");
  for (ttt = 0; ttt < generations; ttt++)
    {
        
	r = ( (double)rand() / ((double)(RAND_MAX)+(double)(1)) );
        
	if(r < 0.1)
	{ mom_gene[ttt] = r;}
        else
        { mom_gene[ttt]= -r;}
  //    printf("\n %f", mom_gene[ttt]);
    }

  for (ttt = generations; ttt > 0; ttt--)
    {
	r = ( (double)rand() / ((double)(RAND_MAX)+(double)(1)) );
     if(r < 0.1)
	{ dad_gene[ttt] = r;}
        else
        { dad_gene[ttt]= -r;}
 //    printf("\n%f", dad_gene[ttt]);
     }

printf("\n");
  for (ttt = 0; ttt < generations; ttt++)
{    printf("\n %f", dad_gene[ttt]);
    printf("\n %f", mom_gene[ttt]);
}

return;
}




int main(void)
{

//int seed = 456739853;
//  srand48(time(NULL));


populate(10);
return 0;
}

glad you got it :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.