Thread: Strange problem

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    71

    Strange problem

    Hello there,

    To get to the point, I have this function:

    Code:
    double fractNum(int limit)
    {
        int x = rand() % (limit + 1);
        double y = rand() % ACCURACY;
        y/= ACCURACY;
        y+=x;
        return y;
    }
    where ACCURACY equals 1000
    srand is seeded like this:

    Code:
    time_t seconds;
    
    time(&seconds);
    srand((unsigned int) seconds);
    the function is called like this:

    Code:
    object[x].realX = fractNum(HIGHT);
    object[x].realY = fractNum(LENGTH);
    where HIGHT and LENGTH equal 10, x is being incremented by a loop, and object is a struct, but that doesn't really matter.

    What this should do is assign a real value to realX/Y, between 0.000 and 9.999. The problem is, instead I get gross values like -282433580....goes on for around 30 digits plus a couple after the decimal point. I have no idea what's going on...the problem seems to be in the random generator, as far as I could tell....anyone see the problem I've missed? :-)

    Thanks,

    G4B3

  2. #2
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    Just a hunch, but before you start maninpulating the value returned by rand(), print it out and see what kind of value is returned. I've seen some compilers return numbers that range from 0.00001 to 1.0000. I think you are assuming that the value you get back is an 'int'??

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    My first guess is that the code which calls fractNum() does not have its prototype available, so it assumes it returns an integer, not a double, and you get weird values. Turn up your warnings all the way.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    71
    I have a prototype. The return value of fractNum() is fine, I've already checked that. And, I'm quoting the FAQ here:

    The prototypes for these two functions are:

    #include <stdlib.h>
    int rand(void);
    void srand(unsigned int seed);
    and cplusplus.com

    Returns a pseudo-random integral number in the range 0 to RAND_MAX.

    Return Value
    An integer value between 0 and RAND_MAX.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by G4B3 View Post
    I have a prototype. The return value of fractNum() is fine, I've already checked that.
    If the return value from fractNum() is fine, then what's the problem?

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Just pointing out that HEIGHT is spelt with an E.
    Have you stepped through the code with a debugger?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    71
    oops, you're right *blush*

    no, and I don't have to. the declaration of realX/y was long double. I can't BELIEVE I overlooked it.... :-O sorry for the bother everyone..*blushes a deeper shade of red*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange problem
    By h3ro in forum Windows Programming
    Replies: 2
    Last Post: 09-02-2008, 12:14 AM
  2. Strange problem with GETLINE
    By wco5002 in forum C++ Programming
    Replies: 13
    Last Post: 07-07-2008, 09:57 AM
  3. Strange problem with classes in header files
    By samGwilliam in forum C++ Programming
    Replies: 2
    Last Post: 02-29-2008, 04:55 AM
  4. Strange problem
    By ~Kyo~ in forum Game Programming
    Replies: 0
    Last Post: 02-14-2006, 10:35 PM