Remove old shuffle header

This commit is contained in:
Justine Tunney 2024-12-30 03:03:32 -08:00
parent c7e3d9f7ff
commit a51ccc8fb1
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
5 changed files with 64 additions and 32 deletions

View file

@ -16,14 +16,23 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/mem/shuffle.internal.h"
#include "libc/stdio/rand.h"
#include "libc/str/str.h"
/**
* Jumbles up string.
* Performs Fisher-Yates shuffle on string in-place to create anagram.
*
* This implementation uses rand() so `srand(time(0))` may be desired.
*/
char *strfry(char *s) {
shuffle(rand, s, strlen(s));
size_t i = strlen(s);
while (i > 1) {
size_t x = rand();
size_t y = rand();
size_t j = ((x << 31) ^ y) % i--;
char t = s[j];
s[j] = s[i];
s[i] = t;
}
return s;
}