洗牌数组 [英] Shuffling an array

查看:128
本文介绍了洗牌数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是什么code是应该做的是洗牌数组,但是我在我得到同样的洗牌(当然通过输入同样未洗牌阵列)阵列运行它每次我想到了函数srand(时间(NULL)); 部分是要确保这一点。如果不是它,我不知道如何使它真正洗牌了。

因此​​,要总结,我需要知道为什么我的code被洗牌数组每次以同样的方式。

 #包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&MATH.H GT;
#包括LT&;&time.h中GT;
诠释主(){    INT N;
    INT米;
    INT TMP;
    INT I;    的printf(请输入元素的数量阵列中:\\ n);
    scanf函数(%d个,&安培; N);    INT巴拉哈[N];
    INT tempbaraja [N];
    的for(int i = 0;我≤(的sizeof(巴拉哈)/ sizeof的(巴拉哈[0]));我++){
        的printf(请输入您的数组的%D元素:\\ n,I);
        scanf函数(%d个,&安培;巴拉哈[I]);
    }
    的printf(Unshuffled数组:\\ n);
    对于(i = 0; I< N;我++){
        的printf(%d个\\ N,巴拉哈[I]);
    }
    的for(int i = 0; I< N;我++){
        tempbaraja [I] =巴拉哈[I]
    }
    的for(int i = 0; I< N;我++){
        函数srand(时间(NULL));
        M =兰特()%N;
        如果(tempbaraja [M] ==巴拉哈[M]){
            TMP =巴拉哈[M]。
            巴拉哈[M] =巴拉哈[I]
            巴拉哈[I] = tmp目录;
        }其他{
        }
    }
    的printf(混洗阵列:\\ n);
    对于(i = 0; I< N;我++){
        的printf(%d个\\ N,巴拉哈[I]);
    }
}


解决方案

您需要移动函数srand(时间(NULL)); 之外循环。

如果你看到,兰特()伪随机的数发生器。 函数srand()是用来提供的种子的基础上随机数将被生成RAND()

如果你每次的时间种子的具有相同时间(NULL)每次调用兰特()<前/ code>, RAND()将是相同的。

要达到预期的效果,就需要播种随机数生成器的一次的使用函数srand()和后来的每次调用兰特(),它会给你的随机数。

请注意:虽然不是强制性的,这是很好的做法,有明确的返回0 年底的main()

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]);
    }   
}

解决方案

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().

这篇关于洗牌数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆