1 /* Generate random permutations.
3 Copyright (C) 2006, 2007 Free Software Foundation, Inc.
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, or (at your option)
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.
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 Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 /* Written by Paul Eggert. */
29 /* Return the ceiling of the log base 2 of N. If N is zero, return
30 an unspecified value. */
36 for (n
--; n
!= 0; n
/= 2)
41 /* Return an upper bound on the number of random bytes needed to
42 generate the first H elements of a random permutation of N
43 elements. H must not exceed N. */
46 randperm_bound (size_t h
, size_t n
)
48 /* Upper bound on number of bits needed to generate the first number
49 of the permutation. */
50 size_t lg_n
= ceil_lg (n
);
52 /* Upper bound on number of bits needed to generated the first H elements. */
55 /* Convert the bit count to a byte count. */
56 size_t bound
= (ar
+ CHAR_BIT
- 1) / CHAR_BIT
;
61 /* From R, allocate and return a malloc'd array of the first H elements
62 of a random permutation of N elements. H must not exceed N.
63 Return NULL if H is zero. */
66 randperm_new (struct randint_source
*r
, size_t h
, size_t n
)
77 v
= xmalloc (sizeof *v
);
78 v
[0] = randint_choose (r
, n
);
85 v
= xnmalloc (n
, sizeof *v
);
86 for (i
= 0; i
< n
; i
++)
89 for (i
= 0; i
< h
; i
++)
91 size_t j
= i
+ randint_choose (r
, n
- i
);
97 v
= xnrealloc (v
, h
, sizeof *v
);