CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2012

    More efficient ways...

    So I've picked up C++ again after being away from it for 10+ years...*sigh* (the mistakes we make lol). I made a random number generator and work as intended.

    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    using namespace std;
    int main()
        //time based randomizer
        //init counter
        int count = 1;
        //init minimum value
        int min = 1;
        //loop for generator
        for(int x=1; x<=25; x++){
            //random number generator equation
            int num = min+(rand()%15000);
            //max number
            //text counter
            cout << count;
            cout << ". ";
            //print random number
            cout << num << endl;
    My question is: Is there a more efficient way to write this? or perhaps a more "correct" way to implement this code? If not...then I'll move on.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003

    Re: More efficient ways...

    Are you trying to get 1/3 or your numbers to be 9999?

  3. #3
    Join Date
    Oct 2012

    Re: More efficient ways...

    No, I can change a few things within the code so that 9999 is not even possible...this code was more of just a "test" to ensure that if any number generated was < 9999 that it set it to 9999 instead of like 10249 or something.

    Best was I can explain is imagine a game with min/max values for certain counters (HP, Damage, etc.) So lets say Min Damage was 1 and Max was 9999 but with all calculations were done they actually had a max of 15,000...but based on their level the program will only allow a max of 9999....

    Now I can ONLY assume because I haven't test it yet...but I'm hoping later I can change a few things so that like

    int num = min+(rand()%15000);
    could look more like

    int num = min+(rand()%variable);
    This way the program would have predetermined values of what "variable" would be but in another section. The same could be said for "min" being a set value for certain conditions, etc etc etc.

    I hope I explained that in a more coherent manner...

  4. #4
    Join Date
    Apr 2000
    Belgium (Europe)

    Re: More efficient ways...

    rand() used in this way isn't even very good as far as "random" goes, you will see a significantly higher amount of times the lower end of the numbers than the higher end ones.
    If you need random numbers in the 0-15000 range, you really need to use something other than the C-lib rand() function.

    There's several much better PRNG's in the newer C++ libs. See the <random> header file.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.