Subject: small hack
To: None <tech-userlevel@netbsd.org>
From: Perry E. Metzger <perry@piermont.com>
List: tech-userlevel
Date: 09/22/1998 18:24:54
In doing some testing a couple of days ago, I needed something to
randomly shuffle a list of files. Unfortunately, jot didn't seem to do
what I wanted, so I wrote the following tiny program. Anyone object to
my adding it to /usr/bin? The whole thing is under 200 lines, with man
page (much less if you ignore copyrights :), and I've wanted this over
and over and over during the last ten years...

# This is a shell archive.  Save it in a file, remove anything before
# this line, and then unpack it by entering "sh file".  Note, it may
# create directories; files and directories will be owned by you and
# have default permissions.
#
# This archive contains:
#
#	Makefile
#	shuffle.1
#	shuffle.c
#
echo x - Makefile
sed 's/^X//' >Makefile << 'END-of-Makefile'
X#	$NetBSD$
X
XPROG=	shuffle
X
X.include <bsd.prog.mk>
END-of-Makefile
echo x - shuffle.1
sed 's/^X//' >shuffle.1 << 'END-of-shuffle.1'
X.\"	$NetBSD$
X.\"
X.\" Copyright (c) 1998
X.\" 	Perry E. Metzger.  All rights reserved.
X.\"
X.\" Redistribution and use in source and binary forms, with or without
X.\" modification, are permitted provided that the following conditions
X.\" are met:
X.\" 1. Redistributions of source code must retain the above copyright
X.\"    notice, this list of conditions and the following disclaimer.
X.\" 2. Redistributions in binary form must reproduce the above copyright
X.\"    notice, this list of conditions and the following disclaimer in the
X.\"    documentation and/or other materials provided with the distribution.
X.\" 3. All advertising materials mentioning features or use of this software
X.\"    must display the following acknowledgement:
X.\"	This product includes software developed for the NetBSD Project
X.\"	by Perry E. Metzger.
X.\" 4. The name of the author may not be used to endorse or promote products
X.\"    derived from this software without specific prior written permission.
X.\"
X.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
X.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
X.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
X.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
X.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
X.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
X.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
X.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
X.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
X.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
X.\"
X.\"
X.Dd September 22, 1998
X.Dt SHUFFLE 1
X.Os
X.Sh NAME
X.Nm shuffle
X.Nd print a random permutation of the command line arguments
X.Sh SYNOPSIS
X.Nm
X.Op Ar -n number
X.Op Ar arg ...
X.Sh DESCRIPTION
XThe
X.Nm
Xprogram prints a random permutation (or
X.Dq shuffle )
Xof its arguments. This can be useful in shell scripts for selecting a
Xrandom order in which to do a set of tasks, view a set of files, etc.
X.Pp
XIf the
X.Ar -n
Xoption is given, its argument is treated as a number, and the program
Xinstead prints a random permutation of the numbers between 0 and the
Xargument.
X.Sh EXAMPLES
X.Bd -literal -offset indent
X$ shuffle a b c d
Xc
Xb
Xd
Xa
X$ shuffle a b c d
Xd
Xb
Xa
Xc
X$ shuffle -n 4
X0
X2
X1
X3
X.Ed
X.Sh SEE ALSO
X.Xr jot 1
X.Sh HISTORY
XThe
X.Nm
Xprogram first appeared in
X.Nx 1.4 .
X.Sh AUTHORS
XWritten by Perry E. Metzger (perry@piermont.com).
X.Sh BUGS
XThe random number generator isn't that great, and thus the
Xpermutations often aren't that great.END-of-shuffle.1
echo x - shuffle.c
sed 's/^X//' >shuffle.c << 'END-of-shuffle.c'
X/*	$NetBSD$	*/
X
X/*
X * Copyright (c) 1998
X * 	Perry E. Metzger.  All rights reserved.
X *
X * Redistribution and use in source and binary forms, with or without
X * modification, are permitted provided that the following conditions
X * are met:
X * 1. Redistributions of source code must retain the above copyright
X *    notice, this list of conditions and the following disclaimer.
X * 2. Redistributions in binary form must reproduce the above copyright
X *    notice, this list of conditions and the following disclaimer in the
X *    documentation and/or other materials provided with the distribution.
X * 3. All advertising materials mentioning features or use of this software
X *    must display the following acknowledgement:
X *	This product includes software developed for the NetBSD Project
X *	by Perry E. Metzger.
X * 4. The name of the author may not be used to endorse or promote products
X *    derived from this software without specific prior written permission.
X *
X * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
X * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
X * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
X * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
X * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
X * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
X * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
X * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
X * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
X * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
X */
X
X#include <err.h>
X#include <stdio.h>
X#include <stdlib.h>
X#include <time.h>
X#include <unistd.h>
X
Xint *get_shuffle(int t)
X{
X	int *shuffle;
X	int i, j, k, temp;
X	
X	if ((shuffle = (int *)calloc(t, sizeof(int))) == NULL)
X		errx(1, "fatal: couldn't allocate array.");
X
X	for (i = 0; i < t; i++)
X		shuffle[i] = i;
X	
X	/*
X	 * This algorithm taken from Knuth, Seminumerical Algorithms,
X	 * page 139.
X	 */
X	for (j = 0; j < t; j++) {
X		k = random()%t;
X		temp = shuffle[j];
X		shuffle[j] = shuffle[k];
X		shuffle[k] = temp;
X	}
X
X	return(shuffle);
X}
X
Xvoid usage(void)
X{
X	errx(1, "usage: shuffle [-n number] [arg ...]");
X}
X
Xint main(int argc, char *argv[])
X{
X	extern char *optarg;
X	extern int optind;
X	int nflag, ch;
X	int *shuffle;
X	int i, t;
X
X	nflag = 0;
X	while ((ch = getopt(argc, argv, "n:")) != -1) {
X		switch(ch) {
X		case 'n':
X			if ((t = atoi(optarg)) <= 0)
X				errx(1, "%d: number is less than zero\n", t);
X			nflag = 1;
X			break;
X		case '?':
X		default:
X			usage();
X		}
X	}
X	argc -= optind;
X	argv += optind;
X
X	if (!nflag)
X		t = argc;
X
X	srandom(getpid() ^ ~getuid() ^ (int)time(NULL));
X
X	shuffle = get_shuffle(t);
X
X	for (i = 0; i < t; i++) {
X		if (nflag)
X			printf("%d\n", shuffle[i]);
X		else
X			printf("%s\n", argv[shuffle[i]]);
X	}
X}
END-of-shuffle.c
exit