memshuffle(3pub)

memshuffle3pub

Make an array be in random order

Usage

#include <publib.h>
void *memshuffle(void *block, size_t elsize, size_t elnum);

Overview

memshuffle will move around the elements of an array in a random fashion. It uses the standard library function rand(3) to get the pseudo-random numbers it needs. The caller must set a suitable seed with srand(3).

Return value

memshuffle returns its first argument.

Examples

To shuffle an integer array one might do the following.

int array[4] = { 1, 2, 3, 4 };
memshuffle(array, sizeof(array[0]), 4);

Releases

16 releases since Debian 1.2 (Rex); the text changed 4 times.

  1. 1.2–1.3
  2. 2.0–2.2
  3. 3.0
  4. 3.1–6.0
  5. 7–13

Bugs

On many systems rand(3) is not of very good quality. However, it is the only random number generator that can be assumed to exist. 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.

Author

Lars Wirzenius ([email protected])

From publib-dev 0.40-4 in Debian 13 (Trixie). Parsed by carta from the page's man source.