rand.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #ifndef __PJ_COMPAT_RAND_H__
  20. #define __PJ_COMPAT_RAND_H__
  21. /**
  22. * @file rand.h
  23. * @brief Provides platform_rand() and platform_srand() functions.
  24. */
  25. #if defined(PJ_HAS_STDLIB_H) && PJ_HAS_STDLIB_H != 0
  26. /*
  27. * Use stdlib based rand() and srand().
  28. */
  29. # include <stdlib.h>
  30. # define platform_srand srand
  31. # if defined(RAND_MAX) && RAND_MAX <= 0xFFFF
  32. /*
  33. * When rand() is only 16 bit strong, double the strength
  34. * by calling it twice!
  35. */
  36. PJ_INLINE(int) platform_rand(void)
  37. {
  38. return ((rand() & 0xFFFF) << 16) | (rand() & 0xFFFF);
  39. }
  40. # else
  41. # define platform_rand rand
  42. # endif
  43. #else
  44. # warning "platform_rand() is not implemented"
  45. # define platform_rand() 1
  46. # define platform_srand(seed)
  47. #endif
  48. #endif /* __PJ_COMPAT_RAND_H__ */