Thread: how to really use random low high in C++

  1. #1
    Banned
    Join Date
    Aug 2017
    Posts
    861

    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
    Last edited by userxbw; 10-05-2017 at 02:58 PM.

  2. #2
    Banned
    Join Date
    Aug 2017
    Posts
    861
    this should suffice.
    Code:
    char * get_random_file()
    {
        int min=0, max=opts.dfile.size() - 1; 
        return ( opts.dfile.at(rand() % max + min) );
    }
    sorry for wasting paper.
    Last edited by userxbw; 10-05-2017 at 03:22 PM.

  3. #3
    Guest
    Guest
    That article is from 13 years ago, as it reads at the top. Nowadays there are dedicated random number facilities in the standard library.
    Code:
    #include <iostream>
    #include <random>
    
    using namespace std;
    
    int main()
    {
        int minimum = 1;
        int maximum = 25;
    
        random_device seed;
        default_random_engine rng(seed());
        uniform_int_distribution<> dist(minimum, maximum);
    
        for(int i = 0; i < 10; ++i) {
            cout << dist(rng) << " ";
        }
    }

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Guest View Post
    That article is from 13 years ago, as it reads at the top. Nowadays there are dedicated random number facilities in the standard library.
    Code:
    #include <iostream>
    #include <random>
    
    using namespace std;
    
    int main()
    {
        int minimum = 1;
        int maximum = 25;
    
        random_device seed;
        default_random_engine rng(seed());
        uniform_int_distribution<> dist(minimum, maximum);
    
        for(int i = 0; i < 10; ++i) {
            cout << dist(rng) << " ";
        }
    }
    that's insane 'ly cool. thanks for that update. now I feel even older.
    so is
    Code:
        random_device seed;
    only called once, like srand((unsigned)time(0));, in main(), or each time all of the rest of that gets called?
    Last edited by userxbw; 10-05-2017 at 04:05 PM.

  5. #5
    Guest
    Guest
    Exactly, only called once to seed. You create the rd object, and then invoke it via ().

    And to get random numbers, you just invoke the distribution object, passing it a random number generator. If you just need a random number when the user clicks, it's no issue to construct these objects repeatedly in place. If you need to get lots of numbers quick, you should instead keep these objects alive and reuse them. Or maybe declaring them static within the function would work too.

    You could also feed it the time as before:
    Code:
    default_random_engine rng(time(nullptr)); // feed in time (in seconds)
    Last edited by Guest; 10-05-2017 at 04:21 PM.

  6. #6
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Guest View Post
    Exactly, only called once to seed. You create the rd object, and then invoke it via ().

    And to get random numbers, you just invoke the distribution object, passing it a random number generator. If you just need a random number when the user clicks, it's no issue to construct these objects repeatedly in place. If you need to get lots of numbers quick, you should instead keep these objects alive and reuse them. Or maybe declaring them static within the function would work too.

    You could also feed it the time as before:
    Code:
    default_random_engine rng(time(nullptr)); // feed in time (in seconds)
    something to play with until I getter figured out.
    (got bigger issue now)

  7. #7
    Banned
    Join Date
    Aug 2017
    Posts
    861

    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.
    Last edited by userxbw; 10-05-2017 at 04:43 PM.

  8. #8
    Registered User
    Join Date
    May 2015
    Posts
    56
    Hello,

    Some files are repeated, 00182 and 00146.
    A random number generator can repeat the same number, they are not unique they are random.
    If you want no repeats, you have to make some sort of list and only add the numbers that are not already in the list.

    Regards

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    mad-hatter brings up a very good point.

    If you have a fixed set of pre-existing files you want to randomize, a shuffle might be a better approach.

  10. #10
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by mad-hatter View Post
    Hello,

    Some files are repeated, 00182 and 00146.
    A random number generator can repeat the same number, they are not unique they are random.
    If you want no repeats, you have to make some sort of list and only add the numbers that are not already in the list.

    Regards
    yeah I know that random is not random and a pattern will show itself too given time.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    mad-hatter isn't talking about "truly" random versus pseudorandom though. The statement would apply either way, i.e., if you want to avoid repetition, you need to either randomly select from a unique pool of values, or whenever you generate a random value, you need to check that it is not among the previously generated values.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by laserlight View Post
    mad-hatter isn't talking about "truly" random versus pseudorandom though. The statement would apply either way, i.e., if you want to avoid repetition, you need to either randomly select from a unique pool of values, or whenever you generate a random value, you need to check that it is not among the previously generated values.
    thanks but that is definitely going on the back burner until I get that other issue fixed with the vector thing.
    I was on a roll then bam ... now I got a figure out how to use a debugger.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. High Low Game with High Scores
    By Bradley Buck in forum C Programming
    Replies: 24
    Last Post: 05-27-2011, 12:42 PM
  2. Replies: 0
    Last Post: 08-25-2010, 08:04 AM
  3. Replies: 4
    Last Post: 11-16-2004, 07:29 AM
  4. Replies: 2
    Last Post: 12-25-2003, 01:31 AM
  5. Replies: 1
    Last Post: 12-14-2002, 01:51 AM

Tags for this Thread