rand() returns 0

im sure im doing something really stupid, but the rand() functions keeps returning just 0, could anyone help?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <fstream>
#include <iomanip>
bool isTri(double,double,double);

int main()
{
    srand((unsigned)time(0));
    double a,b,c,point1,point2;
    point1 = (rand() / RAND_MAX);
    point2 = (rand() / RAND_MAX);
    std::cout.precision(6);
    std::cout.setf(std::ios_base::fixed, std::ios_base::floatfield);
    std::cout.setf(std::ios_base::showpoint);
    std::cout << "here are the two rands: " << point1 << " " << point2;

}

bool isTri(double a,double b,double c)
{

}
The return value of rand has int type. So the arithmetic of integers is used. As any value of rand is usually less than RAND_MAX then

rand() / RAND_MAX is equal to 0.

For example 2 / 5 == 0 for integer numbers.
If you want floating point division you have to explicitly cast at least one of the sides of /.
point1 = (static_cast<double>(rand()) / RAND_MAX);
Last edited on
thanks for the help, got that working. Only problem is one variable is random while the other pretty much stays constant at .09. The code for the two are EXACTLY the same, yet one stays at a small number and the other one is random. why might that be?
Show code to demonstrate your problem.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <fstream>
#include <iomanip>
bool isTri(double,double,double);

int main()
{
    srand((unsigned)time(NULL));
    double a,b,c,point1,point2;

    point2 = static_cast<double>(rand()) / RAND_MAX;

    point1 = static_cast<double>(rand()) / RAND_MAX;

    a = point1;
    b = point2;
    c = 100 - (point1+point2);
    std::cout.precision(2);
    std::cout.setf(std::ios_base::fixed, std::ios_base::floatfield);
    std::cout.setf(std::ios_base::showpoint);
    std::cout << "here are the two rands: " << point1 << " " << point2;

}

bool isTri(double a,double b,double c)
{

}


my output upon running multiple times:
here are the two rands: .55 .13
here are the two rands: .30 .13 
here are the two rands: .92 .13
here are the two rands: .11 .13


i ran the program a lot more times and still turned out with .13

what i noticed*** this only happens to the first number, the second is fine, switching the order of the two variables will also switch which one keeps getting a near constant number.
Strange. The code you posted should not behave like you describe. Are you sure you have compiled and are running the correct code?
1
2
3
4
5
6
srand((unsigned)time(NULL));
    double a,b,c,point1,point2;

    point2 = static_cast<double>(rand()) / RAND_MAX;

    point1 = static_cast<double>(rand()) / RAND_MAX;



Also why the static cast? I thought they were for C++ objects - couldn't one just use an explicit (double) cast?
Last edited on
Topic archived. No new replies allowed.