Trouble changing the range of a vector

I am using a code to randomly shuffle a vector. The code I copied creates a vector with units from 1-9. I need to generate a vector with values ranging from -0.05 to 0.05. How do I create a vector that has these values, that the program can access?
Thanks, here is my code:



// Random shuffler of a vector

#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <ctime>
#include <cstdlib>
using namespace std;

// The random generator
ptrdiff_t randomv (ptrdiff_t i) { return rand()%i;}

// The pointer for the random generator
ptrdiff_t (*p_randomv)(ptrdiff_t) = randomv;

int main () {
srand ( unsigned ( time (NULL) ) );
int vectorA;
vector<int> ;
vector<int>::iterator it;

// values are set in the vector. How come I cant change the size??
for (int i=0.01; i<0.05; ++i) vectorA.push_back(i);

// now the built in random generator is used
random_shuffle ( vectorA.begin(), vectorA.end() );

// using myrandom:
random_shuffle ( vectorA.begin(), vectorA.end(), p_randomv);

// print out content:
cout << "vectorA contains:";
for (it=vectorA.begin(); it!=vectorA.end(); ++it)
cout << " " << *it;

cout << endl;

system("pause");

return 0;
}


1
2
3
4
5
6
7
vector<double> vals;

for (double i = -.05; i < 0; i+=.01) 
	vals.push_back(i);
for (double i = 0; i <= .05; i+=0.01) 
	vals.push_back(i);


There's probably a slicker way of doing it but this will work. You cannot loop through 0.0 though. I tried and it will not put 0 in the vector. It puts a really small number in instead. I'd like to know why, actually.
Last edited on
I modified the program to this:

#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <ctime>
#include <cstdlib>
using namespace std;

// The random generator
ptrdiff_t randomv (ptrdiff_t i) { return rand()%i;}

// The pointer for the random generator
ptrdiff_t (*p_randomv)(ptrdiff_t) = randomv;

int main () {
srand ( unsigned ( time (NULL) ) );
int vectorA;
vector<int> ;
vector<int>::iterator it;

// values are set in the vector. How come I cant change the size??
vector<double> vals;

for (double i = -.05; i < 0; i+=0.01)
vectorA.push_back(i);
for (double i = 0; i <= .05; i+=0.01)
vectorA.push_back(i);

// using myrandom:
random_shuffle ( vectorA.begin(), vectorA.end(), p_randomv);

// print out content:
cout << "vectorA contains:";
for (it=vectorA.begin(); it!=vectorA.end(); ++it)
cout << " " << *it;

cout << "First number of VectorA";

cout << endl;

system("pause");

return 0;
}

Now I get the following errors:


1>c:\documents and settings\danny\my documents\fall 2011\c++\sh random vector calculator\sh random vector calculator\sh random vector calculator.cpp(31): error C2228: left of '.push_back' must have class/struct/union
1> type is 'int'
1>c:\documents and settings\danny\my documents\fall 2011\c++\sh random vector calculator\sh random vector calculator\sh random vector calculator.cpp(33): error C2228: left of '.push_back' must have class/struct/union
1> type is 'int'
1>c:\documents and settings\danny\my documents\fall 2011\c++\sh random vector calculator\sh random vector calculator\sh random vector calculator.cpp(36): error C2228: left of '.begin' must have class/struct/union
1> type is 'int'
1>c:\documents and settings\danny\my documents\fall 2011\c++\sh random vector calculator\sh random vector calculator\sh random vector calculator.cpp(36): error C2228: left of '.end' must have class/struct/union
1> type is 'int'
1>c:\documents and settings\danny\my documents\fall 2011\c++\sh random vector calculator\sh random vector calculator\sh random vector calculator.cpp(36): error C2780: 'void std::random_shuffle(_RanIt,_RanIt)' : expects 2 arguments - 3 provided
1> c:\program files\microsoft visual studio 10.0\vc\include\algorithm(2233) : see declaration of 'std::random_shuffle'
1>c:\documents and settings\danny\my documents\fall 2011\c++\sh random vector calculator\sh random vector calculator\sh random vector calculator.cpp(40): error C2228: left of '.begin' must have class/struct/union
1> type is 'int'
1>c:\documents and settings\danny\my documents\fall 2011\c++\sh random vector calculator\sh random vector calculator\sh random vector calculator.cpp(40): error C2228: left of '.end' must have class/struct/union
1> type is 'int'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Topic archived. No new replies allowed.