gsl_randist__shuffle.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* randist/shuffle.c
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 James Theiler, Brian Gough
  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 3 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. #include "gsl__config.h"
  20. #include <stdlib.h>
  21. #include <math.h>
  22. #include "gsl_rng.h"
  23. #include "gsl_randist.h"
  24. /* Inline swap and copy functions for moving objects around */
  25. static inline
  26. void swap (void * base, size_t size, size_t i, size_t j)
  27. {
  28. register char * a = size * i + (char *) base ;
  29. register char * b = size * j + (char *) base ;
  30. register size_t s = size ;
  31. if (i == j)
  32. return ;
  33. do
  34. {
  35. char tmp = *a;
  36. *a++ = *b;
  37. *b++ = tmp;
  38. }
  39. while (--s > 0);
  40. }
  41. static inline void
  42. copy (void * dest, size_t i, void * src, size_t j, size_t size)
  43. {
  44. register char * a = size * i + (char *) dest ;
  45. register char * b = size * j + (char *) src ;
  46. register size_t s = size ;
  47. do
  48. {
  49. *a++ = *b++;
  50. }
  51. while (--s > 0);
  52. }
  53. /* Randomly permute (shuffle) N indices
  54. Supply an array x[N] with nmemb members, each of size size and on
  55. return it will be shuffled into a random order. The algorithm is
  56. from Knuth, SemiNumerical Algorithms, v2, p139, who cites Moses and
  57. Oakford, and Durstenfeld */
  58. void
  59. gsl_ran_shuffle (const gsl_rng * r, void * base, size_t n, size_t size)
  60. {
  61. size_t i ;
  62. for (i = n - 1; i > 0; i--)
  63. {
  64. size_t j = gsl_rng_uniform_int(r, i+1); /* originally (i + 1) * gsl_rng_uniform (r) */
  65. swap (base, size, i, j) ;
  66. }
  67. }
  68. int
  69. gsl_ran_choose (const gsl_rng * r, void * dest, size_t k, void * src,
  70. size_t n, size_t size)
  71. {
  72. size_t i, j = 0;
  73. /* Choose k out of n items, return an array x[] of the k items.
  74. These items will prevserve the relative order of the original
  75. input -- you can use shuffle() to randomize the output if you
  76. wish */
  77. if (k > n)
  78. {
  79. GSL_ERROR ("k is greater than n, cannot sample more than n items",
  80. GSL_EINVAL) ;
  81. }
  82. for (i = 0; i < n && j < k; i++)
  83. {
  84. if ((n - i) * gsl_rng_uniform (r) < k - j)
  85. {
  86. copy (dest, j, src, i, size) ;
  87. j++ ;
  88. }
  89. }
  90. return GSL_SUCCESS;
  91. }
  92. void
  93. gsl_ran_sample (const gsl_rng * r, void * dest, size_t k, void * src,
  94. size_t n, size_t size)
  95. {
  96. size_t i, j = 0;
  97. /* Choose k out of n items, with replacement */
  98. for (i = 0; i < k; i++)
  99. {
  100. j = gsl_rng_uniform_int (r, n); /* originally n * gsl_rng_uniform (r) */
  101. copy (dest, i, src, j, size) ;
  102. }
  103. }