Skip to main content
gamedev.net
🔒 Locked

Random Float Problem

Started by Ronin Magus Dec 27, 2002 at 3:41 PM 1 replies 200+ views
Original Post
Ronin Magus
Ronin Magus
I'm having a problem with a random float algo I wrote. Check it out:
      
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));
}
  
  
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:
        
    Position temp;
    
    SeedRandTime();
    
    temp = RandPos(0, 0, 15, 15);
    
    cout << "(" << temp.x << ", " << temp.y << ")" << endl;
      
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]
Ronin Magus
Ronin Magus
**Smacks self in the head**

Doh. I didn''t solve the problem, because there wasn''t a problem.

I must have hit myself in the head because, when testing it, instead of having a loop to print out maybe 10 results, I would just run the program over and over. Which reseeded the randgen over and over. Crap.

Don''t look at me!! I''m hideous!!!


"aut viam inveniam aut faciam" - I will either find a way or make one.

MoonStar Projects

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.