Advertisement

07.15.2008 at 03:12PM PDT, ID: 23567840
[x]
Attachment Details

C rand() question

Asked by igor92128 in C Programming Language, C++ Programming Language

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
 
Loading Advertisement...
 
[+][-]07.15.2008 at 03:14PM PDT, ID: 22011613

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.15.2008 at 03:17PM PDT, ID: 22011637

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]07.15.2008 at 03:17PM PDT, ID: 22011642

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.15.2008 at 03:18PM PDT, ID: 22011648

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.15.2008 at 03:19PM PDT, ID: 22011656

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.15.2008 at 03:19PM PDT, ID: 22011663

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.15.2008 at 03:27PM PDT, ID: 22011703

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.15.2008 at 03:32PM PDT, ID: 22011738

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.15.2008 at 03:33PM PDT, ID: 22011749

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.15.2008 at 03:37PM PDT, ID: 22011774

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.15.2008 at 03:43PM PDT, ID: 22011806

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.15.2008 at 11:41PM PDT, ID: 22013682

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: C Programming Language, C++ Programming Language
Sign Up Now!
Solution Provided By: evilrix
Participating Experts: 6
Solution Grade: A
 
 
[+][-]07.17.2008 at 12:54PM PDT, ID: 22029519

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.19.2008 at 12:32PM PDT, ID: 22043460

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628