3 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 James Theiler, Brian Gough
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.
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.
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.
23 #include <gsl/gsl_rng.h>
24 #include <gsl/gsl_randist.h>
26 /* Inline swap and copy functions for moving objects around */
29 void swap (void * base, size_t size, size_t i, size_t j)
31 register char * a = size * i + (char *) base ;
32 register char * b = size * j + (char *) base ;
33 register size_t s = size ;
48 copy (void * dest, size_t i, void * src, size_t j, size_t size)
50 register char * a = size * i + (char *) dest ;
51 register char * b = size * j + (char *) src ;
52 register size_t s = size ;
61 /* Randomly permute (shuffle) N indices
63 Supply an array x[N] with nmemb members, each of size size and on
64 return it will be shuffled into a random order. The algorithm is
65 from Knuth, SemiNumerical Algorithms, v2, p139, who cites Moses and
66 Oakford, and Durstenfeld */
69 gsl_ran_shuffle (const gsl_rng * r, void * base, size_t n, size_t size)
73 for (i = n - 1; i > 0; i--)
75 size_t j = gsl_rng_uniform_int(r, i+1); /* originally (i + 1) * gsl_rng_uniform (r) */
77 swap (base, size, i, j) ;
82 gsl_ran_choose (const gsl_rng * r, void * dest, size_t k, void * src,
83 size_t n, size_t size)
87 /* Choose k out of n items, return an array x[] of the k items.
88 These items will prevserve the relative order of the original
89 input -- you can use shuffle() to randomize the output if you
94 GSL_ERROR ("k is greater than n, cannot sample more than n items",
98 for (i = 0; i < n && j < k; i++)
100 if ((n - i) * gsl_rng_uniform (r) < k - j)
102 copy (dest, j, src, i, size) ;
111 gsl_ran_sample (const gsl_rng * r, void * dest, size_t k, void * src,
112 size_t n, size_t size)
116 /* Choose k out of n items, with replacement */
118 for (i = 0; i < k; i++)
120 j = gsl_rng_uniform_int (r, n); /* originally n * gsl_rng_uniform (r) */
122 copy (dest, i, src, j, size) ;