Mac c++ max size of integer on a Mac

jgrogan

macrumors newbie
Original poster
Jan 14, 2009
3
0
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.
 

gnasher729

macrumors P6
Nov 25, 2005
16,697
3,435
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.
 
Comment

MonkeyCookie

macrumors newbie
Apr 18, 2008
28
0
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.
 
Comment

Detrius

macrumors 68000
Sep 10, 2008
1,621
19
Asheville, NC
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...
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).
 
Comment

jgrogan

macrumors newbie
Original poster
Jan 14, 2009
3
0
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.
 
Comment

Sander

macrumors 6502
Apr 24, 2008
480
7
Check out drand48() - you probably don't need that external library.
 
Comment

MonkeyCookie

macrumors newbie
Apr 18, 2008
28
0
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).
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. :)
 
Comment
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.