1. ## Random Numbers Unix

Does anyone know why this compiles but does not work on a Unix platform using g++? It works fine on a windows box using vs2005. It seems the random number is always 0. Thanks for your help!

Code:
```srand(time(NULL));
int random_integer, random_integernum;
int lowest=0, highest=200, lownum=1, highnum=50;
int range=(highest-lowest)+1;
int rangenum=(highnum-lownum)+1;

cout << "\n# of elements  Height of Tree   Log2(n)" << endl;
Set Random;
for(int i=0; i<20; i++)
{
random_integernum = lownum+int(rangenum*rand()/(RAND_MAX + 1.0));
for(int index=0; index<random_integernum; index++)
{
random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0));
Insert(random_integer, Random.root);
}
cout << "     " << random_integernum << "             " << Maxheight(Random.root);
cout << "            " << log2(double(random_integernum)) << endl;
Makenull(Random.root);
}```

2. >> lownum+int(rangenum*rand()/(RAND_MAX + 1.0));
Perhaps that is causing an overflow of the integer type. RAND_MAX is different on different platforms.

3. I did check on that and RAND_MAX is the same, in fact, this formula is specified in the Unix Man pages.

4. That does look fishy. If RAND_MAX is equal to INT_MAX, then when you increment, it “rolls-over" to zero. Now you are dividing by zero!

5. No, because + 1.0 means that RAND_MAX will be promoted to a floating-point type, which won't have rollover problems.

6. well... you're mulpitplying rangenum by a random number... who says that result can fit into an integer?

dont' forget that's happening before it gets promoted to a floating-point number...

this code
Code:
```#include <iostream>
#include <ctime>

int main()
{
srand(static_cast<short int>(time(0)));
int x;
for(int i=0;i<10;i++)
{
x=50*rand()/(RAND_MAX+1.0);
std::cout<<x<<std::endl;
}
}```
results in this output
Code:
```-1279308866
1370173278
-633427736
445617742
-2042006872
623654194
-806845720
-1524802060
431276324
542628534```
and the rest of the equation:
Code:
```#include <iostream>
#include <ctime>

int main()
{
srand(static_cast<short int>(time(0)));
int x;
for(int i=0;i<10;i++)
{
x=1+int(50*rand()/(RAND_MAX+1.0));
std::cout<<x<<std::endl;
}
}```
results in:
Code:
```xxxxx@MCP ~/Programming/C++ \$ g++ test.cpp -Wall -w -ansi -pedantic -o test.exe && ./test.exe
1
1
1
1
1
1
1
1
1
1```

7. Well, I understand what you are saying, and I did some tests to confirm it, but is there a solution to having a random number within a certain range? And why does this work perfectly on XP with VS2005? I would be content to leave it, but the homework will be graded on a Unix Server....bummer

8. I fixed this by implementing what the Unix Man pages states never to do and that is use low-order bits...

Code:
`random_integer = 1 + (rand()%50)`
This gives a random number between 1 and 50. Since it is only for testing my code and is not actually my code, I guess I will let this slide. If anyone has a better solution, I would appreciate hearing about it.

9. http://www.eternallyconfuzzled.com/articles/rand.html

Our favorite female moderator explains a simple uniform random number algorithm.

EDIT: Owned
|
|
V

10. There is a better solution. Read Prelude's page on using rand().

http://eternallyconfuzzled.com/articles/rand.html