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