I am writing a program that is supposed to generate
10 random numbers all of them with in the range of [-0.5, 0.5] (the data type that is supposed to be produced by the program are 10 random floating point numbers with in the interval [-0.5, 0.5]). But the problem is that I cannot do this correctly. Whenever I run it I keep getting values that are out of range.

Here is what I have:

Output from my program

0) 0.162537
1) -0.0823975 <--Value out of range of [0.5, -0.5]
2) 0.0454712 <--Value out of range of [0.5, -0.5]
3) 0.226471
4) 0.258606
5) 0.129608
6) 0.035675 <--Value out of range of [0.5, -0.5]
7) -0.00393677 <--Value out of range of [0.5, -0.5]
8) -0.128296
9) 0.0968628
10) -0.00238037

Press enter to continue ...

Code the first one is the header file(Market.h), The second one is (Market.cpp), The last is the test file this one runs the program.
---------------------------------------------------------------------------------

``````//(Market.h)
class Market
{
public:
Market();
static void   SetRandom();

static void   PrintPrices();

};``````

----------------------------------------------------------------------------------

``````//(Market.cpp)
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <iomanip>
using namespace std;
#include<cmath>

#include "Market.h"

string input;
const int MAXT = 11; //array size
float r[MAXT]; //array to store the random numbers

void Market : : SetRandom()
{
//this generates my random #'s
srand((unsigned)time(0));
float lowest=-0.5, highest=0.5;
float range=(highest-lowest);

for(int i=0; i<MAXT; i++)
{
r[i] = lowest+float(range*rand()/(RAND_MAX + 1.0));
}
}

void Market : : PrintPrices()
{

for ( int j = 0; j < 11; j++ )
cout << j <<")"<< setw( 13 ) << r[ j ] << endl;

cout<<"\nPress enter to continue ...";
getline( cin, input);

}``````

------------------------------------------------------------------------------------------------

``````//(Test.cpp)
#include "Market.h"

void main()
{
Market : : SetRandom();
Market : : PrintPrices();
}``````
2
Contributors
1
2
Views
10 Years
Discussion Span
Last Post by VernonDozier

Output from my program

0) 0.162537
1) -0.0823975 <--Value out of range of [0.5, -0.5]
2) 0.0454712 <--Value out of range of [0.5, -0.5]
3) 0.226471
4) 0.258606
5) 0.129608
6) 0.035675 <--Value out of range of [0.5, -0.5]
7) -0.00393677 <--Value out of range of [0.5, -0.5]
8) -0.128296
9) 0.0968628
10) -0.00238037

Those all look between -0.5 and 0.5 to me.