C 如何检查以确保生成6个随机数时没有重复?

C 如何检查以确保生成6个随机数时没有重复?,c,arrays,random,numbers,C,Arrays,Random,Numbers,我可以将随机数放入数组,但我不知道如何检查以确保它们不会重复。我打印出代码,但数组中没有数字(不打印任何内容) //将随机数放入数组中 i=0,j=0; srand(时间(空)); 对于(i=0;i

我可以将随机数放入数组,但我不知道如何检查以确保它们不会重复。我打印出代码,但数组中没有数字(不打印任何内容)

//将随机数放入数组中
i=0,j=0;
srand(时间(空));
对于(i=0;i对于(j=1;j小心!这个问题有许多不同的解决方案,它们都有一个或另一个缺点。如果我要快速实现它,我会选择这样的解决方案(不要太多
C
-魔法):

#包括
#包括
#包括
#包括
#定义大小(30)
#定义随机最小值(1)
#定义RAND_MAX(50)
静态整数随机数(整数最小值,整数最大值){
// ...
}
内部主(空){
(无效)srand(时间(无效));
int arr[尺寸];
int=0;
while(使用<大小){
int num=随机数(最小随机数,最大随机数);
bool exists=false;
for(int i=0;i
我希望它能有所帮助:)

就像,你可以这样做,但是固定数量样本的均匀分布对于一个非常简单的散列集来说是完美的。(尽管渐近运行时间可能与
n=6
无关)

#包括/*(s)兰德*/
#包括/*printf*/
#包括/*时钟*/

#将分布的数字作为运行和包括在内,(递归
T(n+1)=T(n)+\mu_{n+1}
,)如果期望值是数字范围除以总样本数,则取一个。

欢迎使用SO。您应该给出一个最小的完整且可验证的示例。请参阅以获取指南。它应该包括所有声明和主声明。@AlainMerigot这只是我的一小部分代码,因为其他部分根本不影响此部分这是gen通常是一个算法问题,有许多不同的解决方案,有不同的权衡。你会在互联网上找到许多不同的方法,只是为了链接这里的一些来源:我认为使用递归然后随机排列值可能是最快的,但是如果两个值相交,你可以排序并重新滚动。我喜欢@Nelp的想法请注意,如果
randNums()
中的数字少于
SIZE
,这将创建一个无休止的循环;如果这不仅仅是在玩弄创意,那么您肯定需要事先验证。
else{low=max+1;high=min;}
似乎混淆了。我怀疑您想要
else{low=max;high=min+1;}
对随机数(一些int,int-max)
和任何数学上
max-min>int-max
你当然是对的情况也有麻烦;使用sarah的“原样”部分,因为问题是关于唯一数,而不是关于随机数的生成。
//puts random numbers into an array
i = 0, j = 0;
srand(time(NULL));
for (i = 0; i < arrSize; i++)
{
  randArr[i] = randNums(1,50);
}

i = 0;
for(i = 0; i < arrSize; i++)
{
  printf("%d ", randArr[i]);
}

printf("\n\n");

//checks to make sure there are no duplicates
i = 0, j = 0, k = 0, temp = 0;
for (i = 0; i < arrSize; i++)
{
  for (j = 1; j <= arrSize;)
  {
    if (randArr[j] == randArr[i])
    {
      for (k = j; k <= arrSize; k++)
      {
        temp = randNums(1,50);
        randArr[k + 1] = temp;
      }
      arrSize--;
    }
    else
      j++;
  }
}

//generates random numbers between the inputed max and min
int randNums(int min, int max)
{
  int result = 0, low = 0, high = 0;
  if (min < max)
  {
    low = min;
    high = max + 1;
  }
  else
  {
    low = max + 1;
    high = min;
  }
  result = (rand() % (high - low)) + low;
  return (result);
}
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define SIZE (30)
#define RAND_MIN (1)
#define RAND_MAX (50)

static int randNums(int min, int max) {
  // ...
}

int main(void) {
  (void) srand(time(NULL));
  int arr[SIZE];
  int used = 0;
  while (used < SIZE) {
    int  num    = randNums(RAND_MIN, RAND_MAX);
    bool exists = false;
    for (int i = 0; i < used; ++i) {
      if (arr[i] == num)
        exists = true;
    }
    if (exists == false)
      arr[used++] = num;
  }
  for (int i = 0; i < SIZE; ++i)
    (void) printf("%d\n", arr[i]);
  return EXIT_SUCCESS;
}
#include <stdlib.h> /* (s)rand */
#include <stdio.h>  /* printf */
#include <time.h>   /* clock */
#include <assert.h> /* assert */

/* Double-pointers are confusing. */
struct Reference { int *ref; };

/* Simple fixed hash set. */
static struct Reference bins[256];
static int nums[6];
static const size_t no_bins = sizeof bins / sizeof *bins,
    no_nums = sizeof nums / sizeof *nums;
static size_t count_num;

/* Uniformly distributed numbers are great for hashing, but possibly clump
 together under linear probing. */
static size_t hash(const int n) { return ((size_t)n * 21) % no_bins; }

/* Linear probing. */
static struct Reference *probe(const int n) {
    size_t bin_index;
    struct Reference *bin;
    assert(sizeof bins > sizeof nums);
    for(bin_index = hash(n); bin = bins + bin_index,
        bin->ref && *bin->ref != n; bin_index = (bin_index + 1) % no_bins);
    return bin;
}

/* Return whether it's a new value. */
static int put_in_set(const int n) {
    struct Reference *bin = probe(n);
    int *num;
    assert(count_num < no_nums);
    if(bin->ref) return 0; /* Already in hash. */
    num = nums + count_num++;
    *num = n;
    bin->ref = num;
    return 1;
}

/* http://c-faq.com/lib/randrange.html */
static int rand_range(const unsigned n) {
    unsigned int x = (RAND_MAX + 1u) / n;
    unsigned int y = x * n;
    unsigned int r;
    assert(n > 0);
    do {
        r = rand();
    } while(r >= y);
    return r / x;
}

/* Generates random numbers between the inputed max and min without
 repetition; [min, max] inclusive. */
static int unique_uniform(const int min, const int max) {
    int n;
    assert(min <= max && (size_t)(max - min) >= count_num);
    do { n = rand_range(max - min + 1) + min; } while(!put_in_set(n));
    return n;
}

int main(void) {
    int n = 6;
    srand((int)clock()), rand(); /* My computer always picks the same first? */
    while(n--) { printf("%d\n", unique_uniform(1, 50)); }
    return EXIT_SUCCESS;
}