c - 洗牌阵列无法正常工作

标签 c

<分区>

我目前正在使用这个函数来混淆数组的所有项:

void shuffleArray(int *array)
{
    int index;
    int buffer
    int randomIndex;
    srand((int)time(NULL));
    //SIZE is my tab's length
    for(index = 0; index < SIZE - 1; index++)
    {
        randomIndex = (index + rand() / (RAND_MAX / SIZE - index) + 1));
        buffer = array[randomIndex];
        array[randomIndex] = array[index];
        array[index] = buffer;
    }
}

但我有一个问题:我的数组的第一个值总是与当前时间的函数相同。

我迷路了,如果你能告诉我正确的方法..

最佳答案

srand() 为程序获取一次相同的随机数 这就是问题所在

srand() — why call it only once?

这有帮助

关于c - 洗牌阵列无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27616375/

相关文章:

c - 在 C 中使用/不使用 float 进行除法

c - 添加两个整数变量并显示输出 C

c++ - UNIX C 编程

c - 指向结构的指针,试图引用其数据成员 - C

c - 函数指针有什么用,我将如何使用它们?

c - 错误: char converts between pointers to integer types with different sign

C编程返回0而指针指向值3

c - 怎样让它更漂亮?

无法从代理获取 QUERY_STRING 到应用程序

c++ - 多播接收器套接字中的重复数据包