0

I want to give an array of a specific length a random number generated to each elements value where the values are varying.

srand(time(NULL));
int n = 8; //n is given for the sake of this example
int r[n];
for (int i = 0; i < n; i++)
{
  r[i] = (rand() % n) + 1;
  for (int j = 0; j < i; j++)
  {
    if (r[i] != r[j])
    {
     return 0;
    }
    else
    {
      while (r[i] == r[j])
      {
        r[i] = (rand() % n) + 1;
      }
    }
}

The problem is that the while loop doesn't go through every single element in the array "r" which most likely will give the array multiple elements with the same value since it only checks for one element and not the ones before.

In conclusion I need help to check for all elements and somehow eliminate the ones already chosen.

Wave
  • 11
  • 3
  • By assigning the index (or index + 1) as the value of each element and shuffling (swapping) them at random, i.e. with random indexes instead of random values. – Weather Vane Jan 02 '23 at 17:56
  • what do you intend by `int r;` followed by `r[i] = ...` ? – jhnc Jan 02 '23 at 18:03
  • 2
    Be clearer. _between_ 0 and n or 0 to n _inclusive_? Your code does neither the random number generation is 1 to n _inclusive_. Either way there is a simpler method. Simply fill the array with monotonically increasing values in the range, then shuffle the array. If there are fewer array elements than possible values, omit a number randomly. If there are fewer values than elements, they cannot all be unique. – Clifford Jan 02 '23 at 18:28
  • 2
    `r` is not an array. This code is not compilable and therefore not the code you are discussing. You need to post _real_ code is we are to tell you what is wrong with it - that code has issues other then the one you are asking a question about, and may not even exhibit the issue you _are_ asking about. – Clifford Jan 02 '23 at 18:37
  • There is about 30 duplicates of this question on this here site. Have you tried a search? – n. m. could be an AI Jan 02 '23 at 21:35

2 Answers2

1

Your description of your requirement is ambiguous, but your code appears to be an attempt at a randomly ordered sequence of values from 1 to n inclusive.

That being the case, a simpler and more deterministic method than picking a random number than checking if it has already been used is simply to generate the values sequentially, then shuffle the array.

    // Create an array of n elements
    int* r = malloc( n * sizeof(*r) ) ;
    
    // Fill the array with values 1 to n
    for( size_t i = 0; i < n; i++ )
    {
        r[i] = i + 1 ;
    }
    
    // Shuffle the array
    for( size_t i = 0; i < n - 1; i++) 
    {
        size_t j = i + rand() / (RAND_MAX / (n - i) + 1);
        int t = r[j];
        r[j] = r[i];
        r[i] = t;
    }
Clifford
  • 88,407
  • 13
  • 85
  • 165
  • Note before commenting, not my shuffle: came from https://benpfaff.org/writings/clc/shuffle.html via https://stackoverflow.com/questions/6127503/shuffle-array-in-c. Read the author's comments and comments to the accepted answer on the random number generation https://stackoverflow.com/questions/6127503/shuffle-array-in-c. Dealing with that is a different question, and may not be critical in this application. The point is to shuffle - the means is up to the reader. – Clifford Jan 02 '23 at 19:51
0

You logic is sound albeit not very efficient.

But you're missing an array and your loops don't work.

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

int main ()
{
    srand(time(NULL));
    int r[8];  // this is an array
    int n = 8; //n is given for the sake of this example
    for (int i=0; i<n; i++)
    {
        r[i] = (rand() % n) + 1;
        for (int j = 0; j < i; j++)
        {
            if (r[i] == r[j])   // if duplicate
            {
                i--;        // go back
                break;      // and try again
            }
        }
    }
    for (int i=0; i<n; i++) printf ("%d,",r[i]);
    printf ("\n");
}
Clifford
  • 88,407
  • 13
  • 85
  • 165
Humpity
  • 152
  • 1
  • 9