how to really use random low high in C++
Ok, I am using this example off the net,
C++ Random Numbers - Software Development | DaniWeb
one worked, the one I need to work does not.
Code:
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
srand((unsigned)time(0));
int random_integer;
int lowest=1, highest=10;
int range=(highest-lowest)+1;
for(int index=0; index<20; index++){
random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0));
cout << random_integer << endl;
}
}
my code:
Code:
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
#include "options.h"
#include "files.h"
int main(int argc, char **argv)
{
srand((unsigned)time(0));
init_options(argc, argv);
for(int index=0; index<20; index++)
{
std::cout << "index = "<< index<<get_random_file() << std::endl;
}
return 0;
}
FUNCTION:
Code:
char * get_random_file()
{
int lowest=0, highest=opts.dfile.size() - 1;
std::cout << "heigest "<< highest <<std::endl;
int range=(highest-lowest)+1;
return ( opts.dfile.at( lowest+int(range*rand()/(RAND_MAX + 1.0)) ) );
}
output
Code:
heigest 713262
index = 0/run/media/userx/3TB-External/wallpaper/vlcsnap/vlcsnap-00042.png
heigest 713262
index = 1/run/media/userx/3TB-External/wallpaper/vlcsnap/vlcsnap-00042.png
heigest 713262
index = 2/run/media/userx/3TB-External/wallpaper/vlcsnap/vlcsnap-00042.png
heigest 713262
index = 3/run/media/userx/3TB-External/wallpaper/vlcsnap/vlcsnap-00042.png
as seen I got files, 713262, they are stored in a vector.
( index is not element index btw , it is the count off the loop )
I am not getting a random number. from what I read only call,
Code:
srand((unsigned)time(0));
once, which I did.
where this works, but I need between min max,
Code:
srand((unsigned)time(0));
int random_integer;
for(int index=0; index<20; index++)
{
random_integer = (rand()%10)+1;
std::cout << opts.dfile.at( random_integer ) << std::endl;
}
results: if you look you'll at the right side numbers you'll
see the files are different. where the first function between min
max are all the same, 00042
Code:
/run/media/userx/3TB-External/wallpaper/vlcsnap/vlcsnap-00079.png
/run/media/userx/3TB-External/wallpaper/vlcsnap/vlcsnap-00020.png
/run/media/userx/3TB-External/wallpaper/vlcsnap/vlcsnap-00213.png
/run/media/userx/3TB-External/wallpaper/vlcsnap/vlcsnap-00105.png
/run/media/userx/3TB-External/wallpaper/vlcsnap/vlcsnap-00196.png
/run/media/userx/3TB-External/wallpaper/vlcsnap/vlcsnap-00087.png
/run/media/userx/3TB-External/wallpaper/vlcsnap/vlcsnap-00105.png
/run/media/userx/3TB-External/wallpaper/vlcsnap/vlcsnap-00213.png
/run/media/userx/3TB-External/wallpaper/vlcsnap/vlcsnap-00087.png
/run/media/userx/3TB-External/wallpaper/vlcsnap/vlcsnap-00007.png
/run/media/userx/3TB-External/wallpaper/vlcsnap/vlcsnap-00146.png
/run/media/userx/3TB-External/wallpaper/vlcsnap/vlcsnap-00182.png
/run/media/userx/3TB-External/wallpaper/vlcsnap/vlcsnap-00087.png
/run/media/userx/3TB-External/wallpaper/vlcsnap/vlcsnap-00105.png
/run/media/userx/3TB-External/wallpaper/vlcsnap/vlcsnap-00182.png
/run/media/userx/3TB-External/wallpaper/vlcsnap/vlcsnap-00146.png
/run/media/userx/3TB-External/wallpaper/vlcsnap/vlcsnap-00087.png
/run/media/userx/3TB-External/wallpaper/vlcsnap/vlcsnap-00166.png
/run/media/userx/3TB-External/wallpaper/vlcsnap/vlcsnap-00146.png
/run/media/userx/3TB-External/wallpaper/vlcsnap/vlcsnap-00182.png
vector works one one set of files, not freshly created ones, its a no go.
I was writing a sort function to sort strings with the sort(v.begin(), v.end(), function-call);
then decided to created some image files with sequential numbering in them so I can use them for a quick look to see if they are sorted,
1,2,3,4,5,6,7 etc... after calling the function.
when I ran that function on the newly created files, using blender to get images with numbers already attached to them, quickly. I get file names when sending them into the vector. I get empty space being printed out then I loop the vector.
I can switch the directory to load off of and it works, then when i change to that test dir it does not print out file names, it is like running a loop with cout<< "\n"; getting just new lines.
The files are valid, I ran them in program that proves it. I have no idea what data or code to show because it is working code. as stated it works on one dir filled with image files just not the newly created ones, where a different program(s) see them just find.
so it is no longer am I sorting properly but, now why it is not picking up the newly created files like it should?
Opps posted this in the wrong page, dang tabs on web browser. wrong tab.