First,im sorry my bad english.I hope,i can tell my problem.

there is a string: dizi[5]={1,2,3,4,5}

and when program run, i want to change random place of members.i mean;
first time program running:
dizi[5]={2,4,1,3,5}

second time program running:
dizi[5]={5,3,1,2,4}

3th program running:
dizi[5]={4,2,1,3,5}
.
.
.
etc..
always changes random.how i can do it?

Dani AI

Generated

As described, the goal is to get a different ordering of the same five values each run. was right to put the work in an initialization function and correctly noted you can instead randomize the display order (shuffle an index array) if you need to keep the original array intact. The standard, unbiased way to produce a random permutation is the Fisher-Yates shuffle (see Fisher-Yates shuffle).

Here is a simple, practical C implementation you can drop into your program:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void shuffle(int *a, size_t n) {
    if (n < 2) return;
    for (size_t i = n - 1; i > 0; --i) {
        size_t j = (size_t)(rand() / (RAND_MAX + 1.0) * (i + 1));
        int tmp = a[i];
        a[i] = a[j];
        a[j] = tmp;
    }
}

int main(void) {
    int dizi[] = {1, 2, 3, 4, 5};
    size_t n = sizeof dizi / sizeof dizi[0];

    srand((unsigned)time(NULL)); // seed once at startup
    shuffle(dizi, n);

    for (size_t k = 0; k < n; ++k)
        printf("%d%c", dizi[k], k + 1 == n ? '\n' : ' ');
    return 0;
}

Notes and troubleshooting:

  • Call srand once at program start. If you omit it you’ll get the same order every run. Seeding repeatedly (e.g., inside shuffle) can also cause bad results.
  • rand() % (i+1) is common but can produce slight bias; the expression above reduces that for small arrays. For cryptographic or high-quality randomness prefer platform functions like arc4random_uniform or thread-safe random_r.
  • If you want to preserve dizi, create an index array 0..n-1 and shuffle that, then print dizi[index[i]].
  • Common bugs: off-by-one when choosing j, wrong seed usage, or using signed/unsigned types incorrectly.

Thanks to , , and for the helpful hints in the thread.

Recommended Answers

All 6 Replies

I assume you want to randomize these values on startup? if so, then don't initialize the dizi[5] array on startup, but have an initialization function that will set the values randomly. IE,

void randomizeArray(int array*, int arraySize)
{
    for (int i = 0; i < arraySize; i++)
    {
        array[i] = some_unique_random_value_between_1_and_5;
    }
}
int main(void)
{
    int dizi[5];
    randomizeArray(dizi, 5);
.
.
.
    return 0;
}

void randomizeArray(int array*, int arraySize) //should be *array
just correcting trivial details :)

Actually, should be void randomize (int* array, int arraySize)... :-) Thanks. Keyboard keeps changing what I am typing!

Actually, should be void randomize (int* array, int arraySize)... :-) Thanks. Keyboard keeps changing what I am typing!

void randomize(...), maybe you meant void randomizeArray(...) as to be consistent with the code you posted

Sorry just nitpicking here :)

how?manuel?
i want to otomaticly.
program must choose random in dizi[5].

If you meant that (i.e. the above soln.) then fine...
but somehow i feel that you don't want random elements but you just want to display the same elements randomly (i observe that 1,2,3,4,5 were not changing)

In that case, you can randomize array indexes while displaying the elements.

Ok, enough hints...try to come up with some code with all the ideas you have gathered so far!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.