I have the code below:
#include <stdio.h>
#include <stdlib.h>
int didThisHit (int HitRolls, int ResistRolls);
void main()
{
int total = 0;
int meta_counter= 0;
for (meta_counter = 0; meta_counter < 1000; meta_counter++)
{
total += didThisHit(6, 2);
}
printf (" 6 Dice resisted by 2 gave %d hits out of 1000 tries", total);
total = 0;
for (meta_counter = 0; meta_counter < 1000; meta_counter++)
{
total += didThisHit(5, 1);
}
printf (" 5 Dice resisted by 1 gave %d hits out of 1000 tries", total);
}
int didThisHit (int HitRolls, int ResistRolls)
{
int hits = 0;
int i = 1;
int resists = 0;
double j = rand();
int counter = 0;
// roll for hits
for (i = 1; i <= HitRolls; i++)
{
if (j <= .7)
{
hits++;
}
}
// re-roll
if (counter < HitRolls)
{
if (j <= .7)
{
counter++;
}
}
// roll for resists
for (i = 1; i <= ResistRolls; i++)
{
if (j <= .7)
{
hits++;
}
}
if ((hits - resists) > 4)
return (1);
else
return (0);
}
But when I compile I get this error message: incompatible types in initializer
'double j = rand()'
I've tried declaring rand() as a int also but that doesn't work. What is rand() returning and why is it not working? When I change .7 to 7 it compiles.
I thought rand() returned something between 0 and 1 if no parameter was supplied. Thats what Im trying to return.
Thanks,
Igor
Start Free Trial