Shuffling an array

给你一囗甜甜゛ 提交于 2019-12-12 01:54:55

问题


What this code is supposed to do is shuffle an array, nonetheless every time I run it on a I get the same "shuffled" array (of course by inputting the same un-shuffled array) I thought the srand(time(NULL)); part was going to make sure of that. If that is not it I don’t know how to make it really shuffle it.

So to sum up I need to know why my code is shuffling the array the same way every time.

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


int main(){

    int n;
    int m;
    int tmp;
    int i;

    printf("Please input the number of elements in your array:\n"); 
    scanf("%d", &n);

    int baraja[n];
    int tempbaraja[n];
    for (int i = 0; i < (sizeof(baraja)/sizeof(baraja[0])); i ++){
        printf("Please input the %d element of your array:\n",i);
        scanf("%d",&baraja[i]);
    }
    printf("Unshuffled array:\n");
    for (i=0;i < n;i++) {
        printf(" %d \n",baraja[i]);
    }
    for (int i = 0; i < n; i ++){ 
        tempbaraja[i] = baraja[i];
    }
    for (int i = 0; i < n; i ++){ 
        srand(time(NULL));
        m = rand() % n; 
        if (tempbaraja[m] == baraja[m]){ 
            tmp = baraja[m];
            baraja[m] = baraja[i];
            baraja[i] = tmp;
        }else{
        }
    } 
    printf("Shuffled array:\n");
    for (i=0;i < n;i++) {
        printf(" %d \n",baraja[i]);
    }   
}

回答1:


You need to move srand(time(NULL)); outside the for loop.

If you see, rand() is a pseudo-random number generator. srand() is used to provide the seed based on which the random number will be generated by rand().

If every time you seed with the same time(NULL) before each call to rand(), each outcome of rand() is going to be the same.

To achieve the desired result, you need to seed the random number generator only once using srand() and later on each call of rand(), it will give you the random numbers.

Note: Though it is not mandatory, it's good practice to have an explicit return 0 at the end of main().




回答2:


Get srand(time(NULL)) out of the loop.

From srand reference:

Two different initializations with the same seed will generate the same succession of results in subsequent calls to rand.

So for each iteration you are initializing the random number generator and getting the first item, which is always the same.




回答3:


You're misusing srand, it should only be called once outside of the loop. These is also a chance that the srand and rand implementations may be particularly bad on the platform you're running on, leading to poor performance if the program is re-executed quickly.




回答4:


Alright, I'll answer the followup question. Why is srand() in a loop bad?

The rand() function produces a sequence of acceptably random numbers, without needing srand() at all. The problem is that it's a fixed sequence, so every time you run the program, you'll get the same set of "random" numbers.

The goal of srand(time(NULL)) is to pick a starting value different from any previous starting value. The time() function call is only being used to pick a different starting point, with the drawback that the result only changes once per second. Running the program twice in the same second can be a problem with programs launched from a script file or batch job, but usually not when launched from keyboard or mouse events.

So, using srand(time(NULL)) in the loop has one major evil effect. You only get one new random number every wall-clock second.

Even if you had a high-precision timer to sample from, or used:

    unsigned counter = time(NULL);
    srand(counter++);

..to choose a different starting value, there are still problems. The distribution of random number chosen from sequential starting points is not as "random" as the overall sequence, and may be quite bad for some generators. If you had a source of starting points that was random enough to give good result from srand();rand();, then you probably wouldn't need rand() at all. You could just use those starting values directly!

Finally, srand() slows down your program. Visual C++ in particular has notoriously slow rand() and srand(), and MinGW uses that library too. (The technical issue is due to storing the seed value in thread local storage, which takes over 100 instructions to find on each call.) However, even a statically linked non-thread-safe version involves an extra call and return that you don't need.

Those are the major reasons that I know of for not using srand() in a loop. Most developers would be happy with "it's not necessary".




回答5:


The glibc srand() manual page, which states: "The srand() function sets its argument as the seed for a new sequence of pseudo-random integers to be returned by rand(). These sequences are repeatable by calling srand() with the same seed value." Specifically your program potentially violated the clause : "..sequences are repeatable by calling srand() with the same seed value." because it could seed rand() more than once per second with the time -- resulting in identical pseudo-random number sequences.



来源:https://stackoverflow.com/questions/28517879/shuffling-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!