1. ## Strange problem

Hello there,

To get to the point, I have this function:

Code:
```double fractNum(int limit)
{
int x = rand() &#37; (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. 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. 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. 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. Originally Posted by G4B3 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. Just pointing out that HEIGHT is spelt with an E.
Have you stepped through the code with a debugger? 7. 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 