Thread: Editing an include.h file

  1. #1
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Lathrop, CA

    Unhappy Editing an include.h file

    Background: I made a copy of stdlib.h file and edited RAND_MAX to a much lower number so that my program generates random numbers in the range of my interest... i.e. max = 47.

    Here is a snip of the code. When I compiled & ran my program, it generated the same thing as if I had not modified anything.

     #include <stdio.h>
    #include <time.h>
    #include "mstdlib.h"
    I put the mstdlib.h file in the directory that my compiler goes to when specifying and external .h file. No compiler errors, but I still get the same random numbers up to the old RAND_MAX.

    Here is a piece of my modified stdlib.h:

     /* Maximum value that can be returned by the rand function. */
    #define RAND_MAX 0x002f
     * Maximum number of bytes in multi-byte character in the current locale
     * (also defined in ctype.h).
    Now 2f is a hex value equivalent to 47.

    How is it possible that when building the solution I still get the same high numbers as if I were using the original stdlib.h file?

    I even closed my compiler & reloaded everything. I have already found a solution for limiting the rand() return value on this board: Tutorial: Generating Random Numbers

    But now my question is why didn't using a modified stdlib.h didn't do the trick? I have spent the last 3 hours researching this on the FAQ and Google.

    Maybe I'm using the wrong search strings here on the FAQ. Thanks!
    Last edited by Char*Pntr; 08-15-2010 at 09:49 PM. Reason: typo

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    The rand() function does not consult RAND_MAX (at least, it's not required to by the standard, and it probably wont in any serious implementation). It's there for your reference only. It isn't intended to modify the way random numbers are generated. In other words, your implementation / system has a certain way it generates random numbers. The maximum possible output of that implementation is represented by RAND_MAX, so that you can refer to that limit in your code. Assigning a different value to RAND_MAX doesn't mean the implementation will now act accordingly.

    Also, any time you find yourself editing standard header files, that's a very good sign that you're really doing something the wrong way. They are not intended to be used that way. They are there to provide the functionality required by the standard, and if you need to modify that, you need to really know what you're doing. I can't think of a situation in which it isn't better to just write your own function to do what you need. Not only that, but the implementation is compiled into a library already. You making a copy of a header file and modifying it is really missing what's actually going on. The header files are only available to your program so that they can get copies of the definitions to compile itself.

    If you want random numbers in a certain range, you can just get numbers raw from the rand() function, and then mod the result with the upper limit of your ranger (i.e. rand() % 6, for a random number from 0 to 5). There's a very slight bias that you can get rid of with some simple arithmetic - and there was a long thread on this very recently - I'll see if I can find you the link...

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    Here is a recent discussion on generating random numbers whilst avoiding bias, which is likely the one Sean was thinking of:
    need random number generator
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Lathrop, CA

    Smile Thank you!

    Thank you all for helping me. I was only tinkering with the stdlib.h as an experiment. I would never, ever modify a .h file just to get my program to work. But this was all worth it. So, it turns out that RAND_MAX is not actually consulted during the linking process... I will research this further, because if I comment out the "stdlib.h" file, I get errors everywhere rand() is used. So I figured the .h file there is used by the linking process, not just to give me information. I've ordered all of the C books recommended by the FAQ here. I now have C Primer Plus, and the Ritchie & Kernighan "The C Programming Language," and the other 2 will arrive soon. I'm currently 1/2 through "Sams Teach Yourself C in 21 Days" and will read the new books asap. I'm assuming they will shed more light on the include .h files and how they're used. Again thank you and unless someone else has a comment about how the stdlib.h file is used, this thread is completed.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    I just wanted to describe what might happen even if the standard library's code referred to RAND_MAX.

    In this case the code for rand() would have been compiled into an object file (.o) and packaged into a library (probably .a or .lib) before being bundled with your compiler. RAND_MAX is a #define, a compile-time constant, so the value of RAND_MAX would be read once and then buried somewhere in the object code.

    If you changed the value of RAND_MAX in your stdlib.h header file at this point, it would not affect the object code for rand() that has already been generated. So rand() would still continue to work as it always did.

    Any time you change compile-time constants, you have to recompile all of the code that might have referred to it in order to have the changes reflected in the actual program.

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Lathrop, CA

    Smile Understand now...

    Thank you dwks, I understand all now. Done deal! :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 10:04 AM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

Tags for this Thread