随机数生成器生成错误的数字集合。

时间:2021-12-22 03:58:22

I'm attempting to make a program that generates an array of random numbers where no two cells contain the same number within a given range.

我正在尝试创建一个程序,它生成一个随机数数组,其中没有两个单元格在给定范围内包含相同的数字。

Example: asking it to make an array of 4 should yield something like: 4 2 1 3, instead what I get is this: 4 2 1360726912 245694014

例子:让它做一个4的数组应该会产生一些类似的东西:4 2 13,而我得到的是这个:4 2 1360726912 245694014。

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

#define TRUE 1
#define FALSE 0

// generates a random number within a given range
int random(int from, int to) {
  int 
    high = to + 1,
    low = from;

  return (rand() % high - low) + low;
}

// returns a random number different from any number in the array
int gen_different(int arr_len, int arr[]) {
  int rand_val = random(1, arr_len);
  int matches = FALSE;
  for(int i = 0; i < arr_len; i++) {
    if(rand_val == arr[i]) matches = TRUE;
  }

  if(matches) gen_different(arr_len, arr);
  return rand_val;
}

// generates an array with a given number of cells, containing all different numbers
void gen_arr(int count, int arr[]) {
  for(int i = 0; i < count; i++)
    arr[i] = gen_different(count, arr);
}

int main() {
  srand(time(NULL));
  printf("Please enter an array number: ");
  int num = 0;
  scanf("%d", &num);
  int* arr[num];
  gen_arr(num, arr);

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

  printf("\n");
  return 0;
}

I get that my issue has something to do with pointers, but I'm not quite sure what I should change to make it work. Any help?

我知道我的问题与指针有关,但我不确定我应该改变什么来让它起作用。任何帮助吗?

4 个解决方案

#1


1  

you are initializing arr[num] as

您正在初始化arr[num]。

int* arr[num]; //array of pointers to integers

it should be like

它应该像

int arr[num]; ////array of integers

#2


1  

One possible problem is you are declaring your array as a pointer to an array

一个可能的问题是,您将您的数组声明为一个指向数组的指针。

int* arr[num];

this is the same as:

这是一样的:

int ** arr;

You should actually use

你应该使用

int arr[num];

As you are expecting an array in your functions.

正如您所期望的函数中的数组。

#3


1  

I think that:

我认为:

 int* arr[num];

is not what you want. Try

不是你想要的。试一试

 int arr[num];

(Note this is not legal C before C99)

(请注意,这在C99之前是不合法的)

#4


0  

Expression:

表达式:

(rand() % high - low) + low

looks suspect as it is the same as rand() % high.

看起来可疑,因为它与rand() %高。

Did you mean:

你的意思是:

(rand() % (high - low)) + low

?

吗?

#1


1  

you are initializing arr[num] as

您正在初始化arr[num]。

int* arr[num]; //array of pointers to integers

it should be like

它应该像

int arr[num]; ////array of integers

#2


1  

One possible problem is you are declaring your array as a pointer to an array

一个可能的问题是,您将您的数组声明为一个指向数组的指针。

int* arr[num];

this is the same as:

这是一样的:

int ** arr;

You should actually use

你应该使用

int arr[num];

As you are expecting an array in your functions.

正如您所期望的函数中的数组。

#3


1  

I think that:

我认为:

 int* arr[num];

is not what you want. Try

不是你想要的。试一试

 int arr[num];

(Note this is not legal C before C99)

(请注意,这在C99之前是不合法的)

#4


0  

Expression:

表达式:

(rand() % high - low) + low

looks suspect as it is the same as rand() % high.

看起来可疑,因为它与rand() %高。

Did you mean:

你的意思是:

(rand() % (high - low)) + low

?

吗?