MEMSHUFFLE(3pub)
π« MEMSHUFFLE(3pub) π«
okay bestie, so this is a page from the C Programmer's Manual, living in section 3pub (that's the publib corner of the manual, iykyk). Pull up a chair, we're shuffling arrays today π
β¨ NAME β¨
memshuffle β make an array be in random order
like, it takes your little array and gives it a whole new vibe, element order wise π¦
πΈ SYNOPSIS πΈ
#include <publib.h>
void *memshuffle(void *block, size_t elsize, size_t elnum);
π DESCRIPTION π
memshuffle will move around the elements of an array in a random fashion. no cap, it just vibes them into a new arrangement β¨
It uses the standard library function rand(3) to get the pseudo-random numbers it needs. so it's not pulling randomness out of thin air, it's borrowing from rand, periodt.
The caller must set a suitable seed with srand(3).
not me stressing this but fr fr: you have to seed it. memshuffle is not gonna do that for you. no seed, no slay. π±
π RETURN VALUE π
memshuffle returns its first argument. that's it, that's the whole return value. it hands you back block like "here bestie, same pointer, new order" π
π§ EXAMPLE π§
To shuffle an integer array one might do the following.
int array[4] = { 1, 2, 3, 4 };
memshuffle(array, sizeof(array[0]), 4);
look at her go πβ¨
π BUGS π
On many systems rand(3) is not of very good quality. tragic, honestly. However, it is the only random number generator that can be assumed to exist. so like, it's the main character by default, not by merit π
Making it possible for the caller to provide an alternate source of random numbers (e.g., via a function pointer) is perhaps too more trouble than its worth. A better way would be for everyone to fix their rand's.
so yeah, the fix is collective. everyone go fix your rand. group project energy, but make it libc π
π₯ SEE ALSO π₯
π AUTHOR π
Lars Wirzenius (lars.wirzenius@helsinki.fi) β the icon, the moment, the legend who wrote this π
that's the whole page bestie, go forth and shuffle responsibly π«Ά
this little number lives in the Publib era of the C Programmer's Manual β MEMSHUFFLE(3pub), mwah π
bye bestie, ily, go seed your rng πβ¨
π¦ where this page comes from
- debian bookworm: publib-dev 0.40-3+b1 Β· BSD-2-Clause
- debian trixie: publib-dev 0.40-4 Β· BSD-2-Clause
- ubuntu jammy: publib-dev 0.40-3build1 Β· BSD-2-Clause
- ubuntu noble: publib-dev 0.40-3build1 Β· BSD-2-Clause
- ubuntu questing: publib-dev 0.40-4
- ubuntu resolute: publib-dev 0.40-4build1 Β· BSD-2-Clause
- ubuntu stonking: publib-dev 0.40-4build1 Β· BSD-2-Clause
- Copyright (c) 1994-2006 Lars Wirzenius. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms