c++ max size of integer on a Mac

Discussion in 'Mac Programming' started by jgrogan, Dec 8, 2009.

  1. jgrogan macrumors newbie

    Joined:
    Jan 14, 2009
    #1
    Hi,

    I'm developing an application on my Mac in generic c++ as I will want to port to other platforms at a later stage.

    I'm using the random number generator - rand() to return an integer. Normally, this should return a number between 1 and 32767. However, it frequently returns numbers way bigger that that.

    In short, I'm trying to generate normally distributed random numbers between 0 and 1.

    Can anyone see where I'm going wrong in my usage or understanding of rand()?

    Thanks,
    John.
     
  2. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    RAND_MAX is the maximum value returned by rand ().

    Google for "C99 Standard Draft" to download a copy of the C99 language standard, which also covers the C portion of Objective-C.
     
  3. MonkeyCookie macrumors newbie

    Joined:
    Apr 18, 2008
    #3
    In C/C++/Objective-C, the size of an int depends on the platform (OS + hardware) that the program is compiled on. 32767 is the maximum value of an int when compiled on a 16-bit platform. An int on a 32-bit platform has a max value of over 2 billion, and and int on a 64-bit platform has some enormous max value. As the above poster said, just use RAND_MAX.

    Get a random number between 0 and 1 by using "(float)rand()/RAND_MAX". The result of rand() will never be larger than RAND_MAX, so division operation will always result in a floating point number between 0 and 1.
     
  4. Detrius macrumors 68000

    Joined:
    Sep 10, 2008
    Location:
    Asheville, NC
    #4
    While your assertion that the size of an int varies from platform to platform is correct, your rule is not. With each of GCC, MSVC, and Intel's compiler in both 32-bit and 64-bit mode, the size of "int" is 32-bits. "size_t" on these platforms is the size of a pointer (though this is also technically not a rule--use sizeof(void*)). That is, 32-bits when compiled for a 32-bit architecture and 64-bits when compiled for a 64-bit architecture.

    If you know you want a 32-bit integer, regardless of platform, you should use "int32_t" or something conceptually similar (e.g. wxInt32). It removes the theoretical ambiguity. If you are allocating a block of memory or referencing an array index, you should use "size_t," as this is the type's purpose (hence the correlation with pointer size).
     
  5. jgrogan thread starter macrumors newbie

    Joined:
    Jan 14, 2009
    #5
    Guys,

    Thanks for all the advice with this. In the end I decided not to use rand(). My application uses tens of millions of random numbers each time it's run and rand() simply didn't provide enough random numbers for my liking, so I found a library that generates random numbers as doubles with a lower likelihood of repeating.

    John.
     
  6. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #6
    Check out drand48() - you probably don't need that external library.
     
  7. MonkeyCookie macrumors newbie

    Joined:
    Apr 18, 2008
    #7
    Ah yes, you're right. I had forgotten that the major compilers did not increase the size of int on a 64-bit platform, just the size of the addresses. I guess I should compile my C++ code on 64-bit platforms more often. :)
     
  8. Detrius macrumors 68000

    Joined:
    Sep 10, 2008
    Location:
    Asheville, NC
    #8
    ...or check out the Boost Random Number Library. If you're already working in C++, Boost is a handy library to have at your disposal. Portions of it (Boost) have been accepted into the next C++ standard.


    http://www.boost.org/doc/libs/1_41_0/libs/random/index.html
     

Share This Page