随机排列生成的不重复 [英] Random array generation with no duplicates

查看:101
本文介绍了随机排列生成的不重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创造的东西,没有重复的值产生一个随机排列。我已经看过其他的答案,但没有人可以帮助我理解。我不能想办法实际生成包含没有重复的随机数。这里是我到目前为止已经试过:

 函数srand(时间(NULL));
INT号[4];为(中间体X = 0; X = 4;!X ++)
{
    号码[X] = 1 +(兰特()%4);
    的printf(%D,数字[X]);
}


  

任何帮助将AP preciated。



解决方案

首先兰特()是generatig随机数而不是wihout重复。

如果你想生成一个随机序列的没有重复兰特()方法是行不通的。

让说你要生成 1000号的数组。在最好的情况下,让说你生成的第一个999号没有重复和最后认为要做的就是生成最后一个数字获得该号码的概率是1/1000 所以这几乎是要采取永远获得生成的。在实践中,只有10个号码会产生很大的麻烦。

最好的方法是通过生成增量的所有号码(或严格单调序列)是的随机他们。在这种情况下会出现的没有重复

这里是如何与10个号码做个例。即使有1000个号码是工作

请注意:从的为Jhon Leehey answer

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&time.h中GT;无效洗牌(INT *改编,为size_t N)
{
    如果(正→1)
    {
        为size_t我;
        函数srand(时间(NULL));
        对于(i = 0; I< N - 1;我++)
        {
          为size_t J = 1 +兰特()/(RAND_MAX /(N - I)+ 1);
          INT T = ARR [J]。
          ARR [J] =改编[I]
          改编[I] = T;
        }
    }
}诠释的main()
{
    INT I;
    INT ARR [10];
    对于(I = 0; I&小于10;我++){
        改编[我] =我;
    }
    洗牌(ARR,10);
    对于(I = 0; I&小于10;我++){
        的printf(%d个,编曲[I]);
    }
}

I am trying to create something that generates a random array with no duplicate values. I've already looked at other answers but none seem to help me understand. I cannot think of a way to actually generate random numbers that contain no duplicates. Here is what i have tried so far:

srand(time(NULL));
int numbers [4];

for (int x=0; x!=4;x++)
{
    numbers[x] = 1 + (rand() % 4) ;
    printf("%d ", numbers[x]);
}

Any help will be appreciated.

解决方案

First of all rand() is generatig random numbers but not wihout duplicates.

If you want to generate a random array without duplicates the rand() method is not working at all.

Let say you want to generate an array of 1000 numbers. In the best case let say you generated the first 999 numbers without duplicates and last think to do is generating the last number. The probability of getting that number is 1/1000 so this is almost going to take forever to get generated. In practice only 10 numbers makes a big trouble.

The best method is to generate all your numbers by incrementation (or strictly monotonic sequence) is shuffle them. In this case there will be no duplicates

Here is an exemple on how to do it with 10 numbers. Even with 1000 numbers it's working.

Note: Suffle function from Jhon Leehey's answer.

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

void shuffle(int *arr, size_t n)
{
    if (n > 1) 
    {
        size_t i;
        srand(time(NULL));
        for (i = 0; i < n - 1; i++) 
        {
          size_t j = i + rand() / (RAND_MAX / (n - i) + 1);
          int t = arr[j];
          arr[j] = arr[i];
          arr[i] = t;
        }
    }
}

int main()
{
    int i;
    int arr[10];
    for (i=0; i<10; i++){
        arr[i] = i;
    }
    shuffle(arr, 10);
    for (i=0; i<10; i++){
        printf("%d ", arr[i]);
    }
}

这篇关于随机排列生成的不重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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