4

I am trying to port some code from linux to windows under cygwin. I find this weird error where 'rand_r is not declared in this scope'. I am using gcc 4.8.1 as x86_64-w64-mingw32 and in it's search path (-v option) it does look in the right directories. The header files are included properly. I am looking for ideas to solve this problem. Is there some fact about cygwin that I am missing and therefore having this problem? Is there a way to check if gcc is in fact touching the required files?

viki.omega9
  • 335
  • 4
  • 12

1 Answers1

4

The rand_r function is considered thread safe, compared to the standard rand function. See man 3 rand_r.

One option is to implement rand_r yourself by wrapping a call to rand. This may or may not be desirable, and as the manual states, rand_r is a fairly weak pseudo-random number generator anyway.

Seeing as you're using C++, why not take a look at the new random number libraries available. They are thread safe, portable, and produce much better random results. This question, while closed as not a real question, still contains some useful information on how to use the library.

If you don't have the C++11 random classes available, Boost also has an implementation.

Community
  • 1
  • 1
Anthony
  • 11,737
  • 9
  • 66
  • 101
  • Thank you. I am going to go ahead and use `rand` until the author of that portion of the code switches to boost. – viki.omega9 Aug 27 '13 at 15:50