Where does the random() function from SDK's urner example come from ?

Mar 31, 2013 at 6:36pm

Where does the random() function from SDK's urner example come from ?

Hello,

Random number generation examples in c most often include the C ANSI rand() and srand() functions. Some people use more sophisticated solutions (see this thread for instance) but those demand external libraries or much complex code. Therefore I do not understand where the random() function used in the SDK’s urner example come from. I can’t see it neither in ext.h nor in ext_obex.h. Is it Darwin-specific stuff ? Why isn’t there any inclusion of a specific header file to use it ?

Thank you in advance.

#67422
Apr 1, 2013 at 6:38am
#242563
Apr 1, 2013 at 8:43am

Thanks Nicolas.

In between I have used the ANSI rand function.

It’s strange that there’s no #include line in the SDK’s urner example. Even stranger: I have also forgotten to include stdlib.h (for rand) and time.h (to seed srand) in my code and it compiled flawlessly. Also the object functions as expected. So I suppose there is a kind of explicit inclusion of these functions. Is the whole C standard library included in such an implicit way ?

#242564
Apr 1, 2013 at 9:56am

Hi,

Yep, tons of headers are included. For instance “ext.h” includes “ext_prefix.h” that includes…

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

… and so on. Surely “time.h” is included somewhere.

That is why, if you don’t use namespace mechanism as in C++ (and you can not in pure C) you should care global name collisions (specially for macros that can lead to silently compiled bugs).
Custom is to prefix each of your name with an arbitrary word (TOTO_MY_MACRO, toto_rand).

The GCC option [GCC_WARN_SHADOW, -Wshadow] can be usefull to avoid that ; but sadly you can not used it with MaxSDK (you get a bunch of warnings if you do that).

#242565
Apr 1, 2013 at 10:07am

Hi,

You could use “rand_r” instead of “rand” for multithread safety.

#242566
Apr 1, 2013 at 2:43pm

Thanks again Nicolas !

#242567

You must be logged in to reply to this topic.