Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Adding to the standard library functions? UNIX
Since reading a recent Hint, I'm compiling a C++ scientific computing package to run on OSX and do some parallel calculations on my dual G4 (MPI and OpenMP). It's all working fine except when I turn on a feature that uses the functions drand48() and sdrand48(), which are a random number generator and a seed respectively. In OSX they aren't defined.

On other *nix systems these functions are defined in stdlib.h, and the corresponding library. Taking into account that I have never compiled a library without explicit instructions - is there an easy way to add these functions without doing anything dangerous to the libraries that came with OSX? Could I replace the standard library safely?

I've currently installed bash and rpm if that would shorten the instruction list at all. Thanks for any help,

Joe.
    •    
  • Currently 4.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[3,267 views]  

Adding to the standard library functions? | 4 comments | Create New Account
Click here to return to the 'Adding to the standard library functions?' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Adding new functions
Authored by: jschimpf on Aug 30, '01 12:45:41PM

This is simple and not-so-simple. First you don't want to mess with any system stuff so all you have to do is add two more files my_rand.c and my_rand.h to the files that you are compiling. Then (here is the not-so-simple part) you have to write srand48 and drand48 (or steal the source from somewhere) and put that c source in the .c file. Then put the function definitions into my_rand.h.
Now all you have to do is put #include "my_srand.h" into each of the files that call these functions and "volia !" your code will link to these and run. This is exactly how stdlib and strings work except Apple in this case has compiled it for you before you compile your code. In this case you just compile these "missing" functions in with your code. If you have any more questions send me email.



[ Reply to This | # ]
Randomness
Authored by: jimr on Aug 30, '01 01:07:23PM
From my *limited* understanding
apparently we do not have a true dev/random on the MacOSX.
on othe systems this is replaced by a set of libraries which are called EGD
?"Entropy Generation Device"?

These sorts of things are necessary and useful for all sorts of applications and especially might be
useful for Encryption or other theoretical mathematics.

your need for functions like rand48()
seem to indicate a 48 bit returned value from the function.

With some rather messy details it might be possible to replicate the functionality and the name space of such a function.

MacOSX does have random() which I am assuming returns a 32 bit random value.

The nature of randomness on computers is that most random functions are only random in that
they return a completely different number each time they are run. However, the sequence of
random numbers might need to be, itself, randomized by adding a new seed value each time you
run the function.


I have read of some interesting approaches for both generation of seeds and generation of
"natural"
sequences of "randomness". for example going into the swap space and copying
from some block. This would be either any block or again some generated address which
would then return some part of the ever changing data in the swap partition. which would never be
the same unless the machine is off. and then convert that data to a numerical string and feed it
to the random generation function.

whether you build a library to handle all of this, or just make a header file to make your application
happy. or first, I would try to change the references in the source to drand48() to random()
and see if it works well enough.


I haven't checked all of my information, nor do I know what you are trying to accomplish exactly.

Otherwise, building a library... just like an application . Declartion files; header files and the
correct output commands in the compiler. Basically, an application revolves around a call to
main().
And I forget.. A library source file needs a call to libmain() or maindll()[in windows].

Feel free to punch this post full of holes as it is an interesting discussion.

Jim







[ Reply to This | # ]
Randomness
Authored by: Anonymous on Aug 31, '01 03:12:45AM

I'm guessing dev/random will be in 10.1, since last I heard on darwin-dev that Louis Gerbarg had a working version of it some time ago.

Don't take my word for it, though ;-)



[ Reply to This | # ]
Use a #define
Authored by: adriaant on Aug 30, '01 10:12:08PM

I used the following code in my source:

#if TARGET_OS_MAC
#define drand48() (((float)rand())/((float)RAND_MAX))
#define srand48(x) (srand(x))
#endif

MacOSX's rand function works properly. I tested it out and I am content with it.



[ Reply to This | # ]