Original Post
I'm having a problem with a random float algo I wrote. Check it out:
Those are the relevant functions, especially RandFloat and RandPos. My Position class is basically just a float x, y and a default constructor and build constructor.
Now, when I run this code:
I get output like this:
(1.51158, 13.764)
(6.43223, 13.7654)
(11.3524, 13.7672)
(1.27262, 13.7686)
You'll notice the x value is pretty random, but the y value just seems to be accumulating, instead of being random too as I had planned.
Any ideas as to why? I just cut/pasted out of my IDE, so any typos on my part could be the problem; though I can't see any.
Plus, if there is a better random float algorithm already written please let me know, I just made this one up so I'm sure it's nowhere near the most effective.
"aut viam inveniam aut faciam " - I will either find a way or make one. MoonStar Projects [edited by - Ronin Magus on December 27, 2002 4:48:21 PM]
inline void SeedRandTime()
{
srand(unsigned(time(NULL)));
}
float RandFloat(float min, float max)
{
if(max != min)
return min + (float(rand()) / RAND_MAX) * (max - min);
// just return the maximum if min and max are the same
return max;
}
Position RandPos(float minx, float miny, float maxx, float maxy)
{
return Position(RandFloat(minx, maxx), RandFloat(miny, maxy));
}
Position temp;
SeedRandTime();
temp = RandPos(0, 0, 15, 15);
cout << "(" << temp.x << ", " << temp.y << ")" << endl;
"aut viam inveniam aut faciam " - I will either find a way or make one. MoonStar Projects [edited by - Ronin Magus on December 27, 2002 4:48:21 PM]