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;
}
3
  • How do you that with a roulette (giving random numbers), a pencil, and a paper? How do you detect (on paper) that 12 numbers are all different? Commented Jan 31, 2021 at 10:16
  • @basile-starynkevitch I Don't know I tried by comparing the number with all the others,each time, but apparently isn't working Commented Jan 31, 2021 at 10:24
  • You first need to be able to do that yourself with a pencil & paper. If you cannot, you won't be able to program the computer doing it! Commented Jan 31, 2021 at 11:30

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

all right but still my program can't detect equals numbers, the main problem is that one
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.
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]);
    }
}

3 Comments

sorry but I was thinking of some simpler code, this looks kinda advanced and I don't understand it
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.
shouldn't you write rand() % (RAND_MAX / (n - i) + 1) instead?
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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.