ref: bb63d0d399128bb8d1ca7633a87b76b251b807b2
parent: 3d2c442bc42050af618706899314414248126476
author: Simon Tatham <anakin@pobox.com>
date: Thu Jul 14 13:37:05 EDT 2005
Introduce a `shuffle' utility function. [originally from svn r6090]
--- a/misc.c
+++ b/misc.c
@@ -196,4 +196,20 @@
}
}
+void shuffle(void *array, int nelts, int eltsize, random_state *rs)
+{
+ char *tmp = smalloc(eltsize);
+ char *carray = (char *)array;
+ int i;
+
+ for (i = nelts; i-- > 1 ;) {
+ int j = random_upto(rs, i+1);
+ if (j != i) {
+ memcpy(tmp, carray + eltsize * i, eltsize);
+ memcpy(carray + eltsize * i, carray + eltsize * j, eltsize);
+ memcpy(carray + eltsize * j, tmp, eltsize);
+ }
+ }
+}
+
/* vim: set shiftwidth=4 tabstop=8: */
--- a/puzzles.h
+++ b/puzzles.h
@@ -223,6 +223,9 @@
void game_mkhighlight(frontend *fe, float *ret,
int background, int highlight, int lowlight);
+/* Randomly shuffles an array of items. */
+void shuffle(void *array, int nelts, int eltsize, random_state *rs);
+
/*
* version.c
*/