Advertisement

Quick problem, quick answer, please..!

Started by January 15, 2001 01:32 PM
1 comment, last by Goblin 24 years ago
  
struct SECTION
{
     float top,bottom,left,right;
};

SECTION newSect()
{
     SECTION newSection;

     newSection.top = -0.7 + ( (rand() % 400) / 1000 );
     // should generate a random number from -0.7 to -0.3

     // code is continued for bottom, left, and right...

     return (newSection);
}

// Later, in the main game loop...

SECTION tunnel_section = newSect();
printf("%f",tunnel_section.top);
  
... and it gives me -0.700000 every single time. I assume this is some sort of problem with converting the right side of the equation to a float, but I''m really not sure how to make it act correctly. Uhh... how? Thanks! ----------------- The Goblin ----------------- "Oh, God..." "Yes?" <- My Response
- The Goblin (madgob@aol.com)
((rand () % 400) / 1000) will always yield 0.

Try this:

(((float) (rand () % 400)) / 1000.f)


Advertisement
*grin*

Thanks!

Amazingly, I was actually able to insert the (float) typecast into enough random places to come out with that exact answer, but I appreciate it, thanks!

Learn something new every day.

I wonder how I would prevent the answer from going more than two decimal places...

-----------------
The Goblin
-----------------
"Oh, God..."
"Yes?" <- My Response
- The Goblin (madgob@aol.com)

This topic is closed to new replies.

Advertisement