fastrand.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * fast pseudo random generation
  3. *
  4. * $Id$
  5. *
  6. * Copyright (C) 2007 iptelorg GmbH
  7. *
  8. * Permission to use, copy, modify, and distribute this software for any
  9. * purpose with or without fee is hereby granted, provided that the above
  10. * copyright notice and this permission notice appear in all copies.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  13. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  14. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  15. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  16. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  17. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  18. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19. */
  20. /*
  21. *History:
  22. *--------
  23. * 2007-06-15 wrapper around isaac (see
  24. * http://www.burtleburtle.net/bob/rand/isaacafa.html) (andrei)
  25. */
  26. #include "fastrand.h"
  27. #include <stdlib.h>
  28. #include "isaac/rand.h"
  29. #define FASTRAND_MAX ((unsigned int)(-1))
  30. static randctx is_ctx;
  31. /* side effect: seeds also random w/ seed */
  32. void fastrand_seed(unsigned int seed)
  33. {
  34. int i;
  35. srandom(seed);
  36. for (i=0; i<RANDSIZ; i++)
  37. is_ctx.randrsl[i]=random();
  38. randinit(&is_ctx, 1);
  39. }
  40. unsigned int fastrand()
  41. {
  42. return rand(&is_ctx);
  43. }
  44. /* generate a random number between 0 and max ( 0 <=r<=max) */
  45. unsigned int fastrand_max(unsigned int max)
  46. {
  47. return fastrand()%(max+1);
  48. }