0

The program should just print out the elements of the array, which stores random integers between 10 and 30. I wanted the numbers to be different from each other, but my program isn't working, what is wrong with it? thanks CODE:

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

int main()
{
    const int N=12;
    int arr[N],i,j;
    srand(time(0));
    for(i=0; i<N; i++)
        arr[i]=10+rand()%20;

    for(i=0; i<N; i++)
    {
        for(j=N-1; j == 0; j--)
        {
            do
            {
                arr[i]=10+rand()%20;
                if(arr[i]!=arr[j])
                    break;
            }
            while(arr[i]==arr[j]);
        }
        printf(">>%d\n",arr[i]);
    }

    return 0;
}
Suman Bhadra
  • 25
  • 1
  • 4
Nome A caso
  • 51
  • 10

3 Answers3

1

The fact that the numbers need to be different from one another means that they are not truly random. You can create another set of numbers with elements 10 through 30 in them. Randomize that list and pull them into your array.

ojblass
  • 21,146
  • 22
  • 83
  • 132
  • all right but still my program can't detect equals numbers, the main problem is that one – Nome A caso Jan 31 '21 at 09:55
  • If you put unique numbers into the initial list: [10, 11, 12, ... 29, 30] and then shuffle the list, the first ten numbers must be unique. – rossum Jan 31 '21 at 18:00
1

C++ version:

const int begin = 10;
const int end = 30;
// creates a vector of 30-10 zeroes
std::vector<int> v(begin-end);
// fill vector with 10, 11, ..., 30.
std::iota (std::begin(v), std::end(v), begin); 
// a source for random seed
std::random_device rd;
// seed this generator with 32-bit number
std::mt19937 g(rd());
// randomly shuffle a vector
std::shuffle(std::begin(v), std::end(v), g);

const int N = 12;
std::vector<int> result(v.begin(), v.begin() + N);

C version:

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

// https://stackoverflow.com/a/6127606/1953079
void shuffle(int *array, size_t n)
{
    if (n <= 1) { return; }
    
    size_t i;
    for (i = 0; i < n - 1; i++) 
    {
        size_t j = i + rand() / (RAND_MAX / (n - i) + 1);
        int t = array[j];
        array[j] = array[i];
        array[i] = t;
    }
}

int main(){
    const int begin = 10;
    const int end = 30;
    const int N = 12;
    srand(time(0));

    // array that contains elements 10, 11...30
    int nums[end-begin];
    for(int i=0;i<end-begin; i++){
        nums[i] = begin+i;
    }

    // randomly shuffle array
    shuffle(nums, end-begin);

    // take first N elements
    int result[N];
    for(int i=0;i<N;i++){
        result[i] = nums[i];
        printf("%d ", result[i]);
    }
}
warchantua
  • 1,154
  • 1
  • 10
  • 24
  • sorry but I was thinking of some simpler code, this looks kinda advanced and I don't understand it – Nome A caso Jan 31 '21 at 11:07
  • In short, you need to generate a sorted array of unique elements 10, 11...30, then randomly shuffle array elements (std::shuffle, or you can find its implementation and resue it), then take first N elements. This is exactly what this code does. – warchantua Jan 31 '21 at 11:10
  • shouldn't you write `rand() % (RAND_MAX / (n - i) + 1)` instead? – Luis Colorado Feb 01 '21 at 15:00
0

Thanks for the help but after some more looking I found what I was doing wrong and now works. code :

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(){
const int N=12;
int arr[N],i,j;
srand(time(0));
for(i=0;i<N;i++)
{
    arr[i]=10+rand()%30;
}
for(i=0;i<N;i++)
{
    for(j=i+1;j<N;j++)
    {
        
        if(arr[i]==arr[j])
        {
            do
            {
                arr[i]=10+rand()%30;
            }
            while(arr[i]==arr[j]);
        }
    }
    printf(">>%d\t",arr[i]);
}
 return 0;
}
Nome A caso
  • 51
  • 10