seq no longer mishandles cases like "seq 0 0.000001 0.000003",
[coreutils/ericb.git] / lib / randperm.c
blobc4438ddfbaa4238169cd42aecbc67bda4a143137
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)
8 any later version.
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. */
21 #include <config.h>
23 #include "randperm.h"
25 #include <limits.h>
27 #include "xalloc.h"
29 /* Return the ceiling of the log base 2 of N. If N is zero, return
30 an unspecified value. */
32 static size_t
33 ceil_lg (size_t n)
35 size_t b = 0;
36 for (n--; n != 0; n /= 2)
37 b++;
38 return b;
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. */
45 size_t
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. */
53 size_t ar = lg_n * h;
55 /* Convert the bit count to a byte count. */
56 size_t bound = (ar + CHAR_BIT - 1) / CHAR_BIT;
58 return bound;
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. */
65 size_t *
66 randperm_new (struct randint_source *r, size_t h, size_t n)
68 size_t *v;
70 switch (h)
72 case 0:
73 v = NULL;
74 break;
76 case 1:
77 v = xmalloc (sizeof *v);
78 v[0] = randint_choose (r, n);
79 break;
81 default:
83 size_t i;
85 v = xnmalloc (n, sizeof *v);
86 for (i = 0; i < n; i++)
87 v[i] = i;
89 for (i = 0; i < h; i++)
91 size_t j = i + randint_choose (r, n - i);
92 size_t t = v[i];
93 v[i] = v[j];
94 v[j] = t;
97 v = xnrealloc (v, h, sizeof *v);
99 break;
102 return v;