aboutsummaryrefslogtreecommitdiff
path: root/misc.c
diff options
context:
space:
mode:
authorSimon Tatham <anakin@pobox.com>2005-07-14 17:37:05 +0000
committerSimon Tatham <anakin@pobox.com>2005-07-14 17:37:05 +0000
commitbb63d0d399128bb8d1ca7633a87b76b251b807b2 (patch)
tree856a8ad7b3f8e615b3957b220b69b11274c390e0 /misc.c
parent3d2c442bc42050af618706899314414248126476 (diff)
downloadpuzzles-bb63d0d399128bb8d1ca7633a87b76b251b807b2.zip
puzzles-bb63d0d399128bb8d1ca7633a87b76b251b807b2.tar.gz
puzzles-bb63d0d399128bb8d1ca7633a87b76b251b807b2.tar.bz2
puzzles-bb63d0d399128bb8d1ca7633a87b76b251b807b2.tar.xz
Introduce a `shuffle' utility function.
[originally from svn r6090]
Diffstat (limited to 'misc.c')
-rw-r--r--misc.c16
1 files changed, 16 insertions, 0 deletions
diff --git a/misc.c b/misc.c
index 025083d..17ec48c 100644
--- a/misc.c
+++ b/misc.c
@@ -196,4 +196,20 @@ void game_mkhighlight(frontend *fe, float *ret,
}
}
+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: */