Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5962251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:04:46+00:00 2026-05-22T19:04:46+00:00

I’m looking for a function in ANSI C that would randomize an array just

  • 0

I’m looking for a function in ANSI C that would randomize an array just like PHP’s shuffle() does. Is there such a function or do I have to write it on my own? And if I have to write it on my own, what’s the best/most performant way to do it?

My ideas so far:

  • Iterate through the array for, say, 100 times and exchange a random index with another random index
  • Create a new array and fill it with random indices from the first one checking each time if the index is already taken (performance = 0 complexity = serious)
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T19:04:47+00:00Added an answer on May 22, 2026 at 7:04 pm

    Pasted from Asmodiel‘s link to Ben Pfaff’s Writings, for persistence:

    #include <stdlib.h>
    
    /* Arrange the N elements of ARRAY in random order.
       Only effective if N is much smaller than RAND_MAX;
       if this may not be the case, use a better random
       number generator. */
    void shuffle(int *array, size_t n)
    {
        if (n > 1) 
        {
            size_t i;
            for (i = 0; i < n - 1; i++) 
            {
              size_t j = i + rand() / (RAND_MAX / (n - i) + 1);
              int t = array[j];
              array[j] = array[i];
              array[i] = t;
            }
        }
    }
    

    EDIT: And here’s a generic version that works for any type (int, struct, …) through memcpy. With an example program to run, it requires VLAs, not every compiler supports this so you might want to change that to malloc (which will perform badly) or a static buffer large enough to accommodate any type you throw at it:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    /* compile and run with
     * cc shuffle.c -o shuffle && ./shuffle */
    
    #define NELEMS(x)  (sizeof(x) / sizeof(x[0]))
    
    /* arrange the N elements of ARRAY in random order.
     * Only effective if N is much smaller than RAND_MAX;
     * if this may not be the case, use a better random
     * number generator. */
    static void shuffle(void *array, size_t n, size_t size) {
        char tmp[size];
        char *arr = array;
        size_t stride = size * sizeof(char);
    
        if (n > 1) {
            size_t i;
            for (i = 0; i < n - 1; ++i) {
                size_t rnd = (size_t) rand();
                size_t j = i + rnd / (RAND_MAX / (n - i) + 1);
    
                memcpy(tmp, arr + j * stride, size);
                memcpy(arr + j * stride, arr + i * stride, size);
                memcpy(arr + i * stride, tmp, size);
            }
        }
    }
    
    #define print_type(count, stmt) \
        do { \
        printf("["); \
        for (size_t i = 0; i < (count); ++i) { \
            stmt; \
        } \
        printf("]\n"); \
        } while (0)
    
    struct cmplex {
        int foo;
        double bar;
    };
    
    int main() {
        srand(time(NULL));
    
        int intarr[] = { 1, -5, 7, 3, 20, 2 };
    
        print_type(NELEMS(intarr), printf("%d,", intarr[i]));
        shuffle(intarr, NELEMS(intarr), sizeof(intarr[0]));
        print_type(NELEMS(intarr), printf("%d,", intarr[i]));
    
        struct cmplex cmparr[] = {
            { 1, 3.14 },
            { 5, 7.12 },
            { 9, 8.94 },
            { 20, 1.84 }
        };
    
        print_type(NELEMS(intarr), printf("{%d %f},", cmparr[i].foo, cmparr[i].bar));
        shuffle(cmparr, NELEMS(cmparr), sizeof(cmparr[0]));
        print_type(NELEMS(intarr), printf("{%d %f},", cmparr[i].foo, cmparr[i].bar));
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.